﻿using HDSmartCommunity.BUS;
using HDSmartCommunity.BUS.MqttBUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.Models;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Xml;
using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
using Timer = System.Timers.Timer;

namespace HDSmartCommunity.DevicePlatform
{
    public class SHGDLine18SerialData
    {
        #region ttys0
        public SerialPort myS0SerialPort_1 = new SerialPort();
        public void OpenS0SerialPort(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            try
            {
                SetS0SerialPortConfig(portName, baudRate, parity, dataBits, stopBits);
                myS0SerialPort_1.Open();
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }

        private Timer SerialPortRecvTimerS0_1;
        private static int byteLength = 40960;
        public byte[] byteSerialPortRecvS0_1 = new byte[byteLength];
        public event EventHandler<SerialPort485_1RecvEventArgs> ReceivedDataEventS0_1;
        private static int pSerialPortRecv_S0_1 = 0;
        public FTLine18ParamInfo fTLine18ParamInfo = new FTLine18ParamInfo();
        public int mqttlog = 0;
        public SHGDLine18MqttClientBUS clientBUS;
        public string topic;
        public List<FaultPoint> faults_list = new List<FaultPoint>();
        public List<Line18Cmd> cmds = new List<Line18Cmd>();
        string query_cmd = string.Empty;
        string query_cmd_2 = string.Empty;
        public string SerialNumber = string.Empty;
        public string DeviceCode = string.Empty;
        public int Simulation = 0;
        public Line18State Line18State = new Line18State();
        public int Interval = 2000;
        public int NetWorkNum = 0;

        public class SerialPort485_1RecvEventArgs : EventArgs
        {
            /// <summary>
            /// 广播 收到的串口数据
            /// </summary>
            public readonly byte[] RecvData_485_1 = new byte[byteLength];

            /// <summary>
            /// 收到数据 的 长度
            /// </summary>
            public readonly int RecvDataLength_485_1;

            /// <summary>
            /// 将 收到的数据 转化成 待广播的数据
            /// </summary>
            public SerialPort485_1RecvEventArgs(byte[] recvData, int recvDataLength)
            {
                recvData.CopyTo(RecvData_485_1, 0);
                RecvDataLength_485_1 = recvDataLength;
            }
        }

        private void SetS0SerialPortConfig(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                portName = $"/dev/{portName}";
            }
            // 串口 参数设置
            myS0SerialPort_1.PortName = portName;
            myS0SerialPort_1.BaudRate = baudRate;
            switch (parity)
            {
                case 0:
                default:
                    myS0SerialPort_1.Parity = Parity.None;
                    break;
                case 1:
                    myS0SerialPort_1.Parity = Parity.Odd;
                    break;
                case 2:
                    myS0SerialPort_1.Parity = Parity.Even;
                    break;
                case 3:
                    myS0SerialPort_1.Parity = Parity.Mark;
                    break;
                case 4:
                    myS0SerialPort_1.Parity = Parity.Space;
                    break;
            }
            myS0SerialPort_1.DataBits = ((4 < dataBits) && (dataBits < 9)) ? dataBits : 8;
            switch (stopBits)
            {
                case 0:
                    myS0SerialPort_1.StopBits = StopBits.None;
                    break;
                case 1:
                default:
                    myS0SerialPort_1.StopBits = StopBits.One;
                    break;
                case 2:
                    myS0SerialPort_1.StopBits = StopBits.OnePointFive;
                    break;
                case 3:
                    myS0SerialPort_1.StopBits = StopBits.Two;
                    break;
            }
            //my485SerialPort_1.ReadTimeout = 200;
            myS0SerialPort_1.RtsEnable = true;
            myS0SerialPort_1.DataReceived += new SerialDataReceivedEventHandler(DataReceived_ttyS0);

            // 串口 接收数据处理定时器 参数设置
            SerialPortRecvTimerS0_1 = new System.Timers.Timer();
            SerialPortRecvTimerS0_1.Interval = 100;
            SerialPortRecvTimerS0_1.AutoReset = false;
            SerialPortRecvTimerS0_1.Elapsed += new ElapsedEventHandler(SPRecvTimer_Tick_ttyS0_1);
        }

        private void SPRecvTimer_Tick_ttyS0_1(object sender, EventArgs e)
        {
            byte[] TemporaryData = new byte[byteLength];
            int TemporaryDataLength = 0;

            if (ReceivedDataEventS0_1 != null)
            {
                byteSerialPortRecvS0_1.CopyTo(TemporaryData, 0);
                TemporaryDataLength = pSerialPortRecv_S0_1;

                ReceivedDataEventS0_1.Invoke(this, new SerialPort485_1RecvEventArgs(TemporaryData, TemporaryDataLength));
                // 数据处理完后，将指针指向数据头，等待接收新的数据
                pSerialPortRecv_S0_1 = 0;
            }
        }

        private void DataReceived_ttyS0(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPortRecvTimerS0_1.Stop();

            try
            {
                byte[] ReadBuf = new byte[myS0SerialPort_1.BytesToRead];
                if (pSerialPortRecv_S0_1 > byteLength - ReadBuf.Length)
                {
                    pSerialPortRecv_S0_1 = 0;
                }
                myS0SerialPort_1.Read(ReadBuf, 0, ReadBuf.Length);
                ReadBuf.CopyTo(byteSerialPortRecvS0_1, pSerialPortRecv_S0_1);
                pSerialPortRecv_S0_1 += ReadBuf.Length;
                SerialDataSaveLogHelper.Log(ToHexs(ReadBuf));

                if (Simulation == 0)
                {
                    for (int i = 0; i < byteSerialPortRecvS0_1.Length; i++)
                    {
                        if (byteSerialPortRecvS0_1[i] == liftid && byteSerialPortRecvS0_1[i + 1] == 0x03 && byteSerialPortRecvS0_1[i + 2] == 0x3A)
                        {
                            int numcount = Convert.ToInt32(byteSerialPortRecvS0_1[i + 2]);
                            byte[] buffers = new byte[numcount + 3];
                            for (int j = 0; j < buffers.Length; j++)
                            {
                                buffers[j] = byteSerialPortRecvS0_1[i + j];
                            }
                            byte[] crc = Crc18(buffers);
                            if (crc[0] == byteSerialPortRecvS0_1[i + numcount + 4] && crc[1] == byteSerialPortRecvS0_1[i + numcount + 3])
                            {
                                Array.Clear(byteSerialPortRecvS0_1, i, numcount + 5);

                                byte[] basic_data = new byte[2];
                                basic_data[0] = buffers[4];
                                basic_data[1] = buffers[3];
                                BitArray bits;
                                bits = new BitArray(basic_data);
                                bool sx = bits.Get(0);
                                bool xx = bits.Get(1);
                                bool yx = bits.Get(2);
                                bool tz = bits.Get(3);
                                bool jx = bits.Get(4);
                                bool gz = bits.Get(5);
                                bool jt = bits.Get(6);
                                bool y_ms = bits.Get(7);
                                bool jnyx_ms = bits.Get(8);
                                bool dsyx_ms = bits.Get(9);
                                bool gsyx_ms = bits.Get(10);
                                bool qsyx_ms = bits.Get(11);
                                bool bp_ms = bits.Get(12);
                                bool zdyx_ms = bits.Get(13);
                                bool sxyx_ms = bits.Get(14);
                                bool lxyx_ms = bits.Get(15);
                                byte[] faultdata = new byte[14];
                                for (int k = 0; k < 14; k++)
                                {
                                    faultdata[k] = buffers[5 + k];
                                }
                                string binaryString1 = string.Join("", Array.ConvertAll(faultdata, b => Convert.ToString(b, 2).PadLeft(8, '0')));
                                string value = binaryString1.Substring(96, 16) + binaryString1.Substring(80, 16) + binaryString1.Substring(64, 16) + binaryString1.Substring(48, 16) + binaryString1.Substring(32, 16) + binaryString1.Substring(16, 16) + binaryString1.Substring(0, 16);
                                string code = string.Empty;
                                for (int m = 0; m < 112; m++)
                                {
                                    if (value.Substring(m, 1) == "1")
                                    {
                                        List<FaultPoint> list = faults_list.FindAll(delegate (FaultPoint e) { return e.id == 112 - m; });
                                        if (list != null && list.Count > 0)
                                        {
                                            code += list[0].faultCode + ",";
                                        }
                                    }
                                }
                                if (!string.IsNullOrEmpty(code))
                                {
                                    code = code.Substring(0, code.Length - 1);
                                }

                                //运行时间40009-40010
                                byte[] sj_data = new byte[4];
                                sj_data[0] = buffers[20];
                                sj_data[1] = buffers[19];
                                sj_data[2] = buffers[22];
                                sj_data[3] = buffers[21];
                                int time = BitConverter.ToInt32(sj_data, 0);

                                double time_sec = time / 3600.0;
                                float time_hour = (float)Math.Round(time_sec, 1);
                                //运行次数40011-40012
                                byte[] cs_data = new byte[4];
                                cs_data[0] = buffers[24];
                                cs_data[1] = buffers[23];
                                cs_data[2] = buffers[26];
                                cs_data[3] = buffers[25];
                                int num = BitConverter.ToInt32(cs_data, 0);

                                //解析抱闸臂制动器（左1）温度
                                byte[] bzb_left1_wd_data = new byte[2];
                                bzb_left1_wd_data[0] = buffers[54];
                                bzb_left1_wd_data[1] = buffers[53];
                                int bzb_left1_wd_value = GetTemperatureValue(bzb_left1_wd_data);

                                //解析抱闸臂制动器（左2）温度
                                byte[] bzb_left2_wd_data = new byte[2];
                                bzb_left2_wd_data[0] = buffers[56];
                                bzb_left2_wd_data[1] = buffers[55];
                                int bzb_left2_wd_value = GetTemperatureValue(bzb_left2_wd_data);

                                //解析抱闸臂制动器（右1）温度
                                byte[] bzb_right1_wd_data = new byte[2];
                                bzb_right1_wd_data[0] = buffers[58];
                                bzb_right1_wd_data[1] = buffers[57];
                                int bzb_right1_wd_value = GetTemperatureValue(bzb_right1_wd_data);

                                //解析抱闸臂制动器（右2）温度
                                byte[] bzb_right2_wd_data = new byte[2];
                                bzb_right2_wd_data[0] = buffers[60];
                                bzb_right2_wd_data[1] = buffers[59];
                                int bzb_right2_wd_value = GetTemperatureValue(bzb_right2_wd_data);


                                long ts = DateTimeOffset.Now.ToUnixTimeSeconds();

                                Line18ZDFT_SX_ZT line18ZDFT_SX_ZT = new Line18ZDFT_SX_ZT();
                                line18ZDFT_SX_ZT.ts = ts;
                                line18ZDFT_SX_ZT.value = sx;
                                fTLine18ParamInfo.ZDFT_SX_ZT = line18ZDFT_SX_ZT;

                                Line18ZDFT_XX_ZT line18ZDFT_XX_ = new Line18ZDFT_XX_ZT();
                                line18ZDFT_XX_.ts = ts;
                                line18ZDFT_XX_.value = xx;
                                fTLine18ParamInfo.ZDFT_XX_ZT = line18ZDFT_XX_;

                                Line18ZDFT_YX_ZT line18ZDFT_YX_ZT = new Line18ZDFT_YX_ZT();
                                line18ZDFT_YX_ZT.ts = ts;
                                line18ZDFT_YX_ZT.value = yx;
                                fTLine18ParamInfo.ZDFT_YX_ZT = line18ZDFT_YX_ZT;

                                Line18ZDFT_TZ_ZT line18ZDFT_TZ_ZT = new Line18ZDFT_TZ_ZT();
                                line18ZDFT_TZ_ZT.ts = ts;
                                line18ZDFT_TZ_ZT.value = tz;
                                fTLine18ParamInfo.ZDFT_TZ_ZT = line18ZDFT_TZ_ZT;

                                Line18ZDFT_JX_BJ line18ZDFT_JX_BJ = new Line18ZDFT_JX_BJ();
                                line18ZDFT_JX_BJ.ts = ts;
                                line18ZDFT_JX_BJ.value = jx;
                                fTLine18ParamInfo.ZDFT_JX_BJ = line18ZDFT_JX_BJ;

                                Line18ZDFT_GZ_DM line18ZDFT_GZ_DM = new Line18ZDFT_GZ_DM();
                                line18ZDFT_GZ_DM.value = code;
                                line18ZDFT_GZ_DM.ts = ts;
                                fTLine18ParamInfo.ZDFT_GZ_DM = line18ZDFT_GZ_DM;

                                Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ = new Line18ZDFT_GZ_BJ();
                                line18ZDFT_GZ_BJ.ts = ts;
                                line18ZDFT_GZ_BJ.value = gz;
                                fTLine18ParamInfo.ZDFT_GZ_BJ = line18ZDFT_GZ_BJ;

                                Line18ZDFT_JT_BJ line18ZDFT_JT_BJ = new Line18ZDFT_JT_BJ();
                                line18ZDFT_JT_BJ.ts = ts;
                                line18ZDFT_JT_BJ.value = jt;
                                fTLine18ParamInfo.ZDFT_JT_BJ = line18ZDFT_JT_BJ;

                                Line18ZDFT_TX_BJ line18ZDFT_TX_BJ = new Line18ZDFT_TX_BJ();
                                line18ZDFT_TX_BJ.ts = ts;
                                line18ZDFT_TX_BJ.value = true;
                                fTLine18ParamInfo.ZDFT_TX_BJ = line18ZDFT_TX_BJ;

                                Line18ZDFT_Y_MS line18ZDFT_Y_MS = new Line18ZDFT_Y_MS();
                                line18ZDFT_Y_MS.value = y_ms;
                                line18ZDFT_Y_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_Y_MS = line18ZDFT_Y_MS;

                                Line18ZDFT_JNYX_MS line18ZDFT_JNYX_MS = new Line18ZDFT_JNYX_MS();
                                line18ZDFT_JNYX_MS.value = jnyx_ms;
                                line18ZDFT_JNYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_JNYX_MS = line18ZDFT_JNYX_MS;

                                Line18ZDFT_DSYX_MS line18ZDFT_DSYX_MS = new Line18ZDFT_DSYX_MS();
                                line18ZDFT_DSYX_MS.value = dsyx_ms;
                                line18ZDFT_DSYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_DSYX_MS = line18ZDFT_DSYX_MS;

                                Line18ZDFT_GSYX_MS line18ZDFT_GSYX_MS = new Line18ZDFT_GSYX_MS();
                                line18ZDFT_GSYX_MS.value = gsyx_ms;
                                line18ZDFT_GSYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_GSYX_MS = line18ZDFT_GSYX_MS;

                                Line18ZDFT_QSYX_MS line18ZDFT_QSYX_MS = new Line18ZDFT_QSYX_MS();
                                line18ZDFT_QSYX_MS.value = qsyx_ms;
                                line18ZDFT_QSYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_QSYX_MS = line18ZDFT_QSYX_MS;

                                Line18ZDFT_BP_MS line18ZDFT_BP_MS = new Line18ZDFT_BP_MS();
                                line18ZDFT_BP_MS.value = bp_ms;
                                line18ZDFT_BP_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_BP_MS = line18ZDFT_BP_MS;

                                Line18ZDFT_ZDYX_MS line18ZDFT_ZDYX_MS = new Line18ZDFT_ZDYX_MS();
                                line18ZDFT_ZDYX_MS.value = zdyx_ms;
                                line18ZDFT_ZDYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_ZDYX_MS = line18ZDFT_ZDYX_MS;

                                Line18ZDFT_SXYX_MS line18ZDFT_SXYX_MS = new Line18ZDFT_SXYX_MS();
                                line18ZDFT_SXYX_MS.value = sxyx_ms;
                                line18ZDFT_SXYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_SXYX_MS = line18ZDFT_SXYX_MS;

                                Line18ZDFT_LXYX_MS line18ZDFT_LXYX_MS = new Line18ZDFT_LXYX_MS();
                                line18ZDFT_LXYX_MS.value = lxyx_ms;
                                line18ZDFT_LXYX_MS.ts = ts;
                                fTLine18ParamInfo.ZDFT_LXYX_MS = line18ZDFT_LXYX_MS;

                                Line18ZDFT_BZBZDQ_WD_LEFT_1 line18ZDFT_BZBZDQ_WD_LEFT_1 = new Line18ZDFT_BZBZDQ_WD_LEFT_1();
                                line18ZDFT_BZBZDQ_WD_LEFT_1.value = Math.Round(bzb_left1_wd_value * 0.1, 1);
                                line18ZDFT_BZBZDQ_WD_LEFT_1.ts = ts;
                                fTLine18ParamInfo.BZBZDQ_WD_LEFT_1 = line18ZDFT_BZBZDQ_WD_LEFT_1;

                                Line18ZDFT_BZBZDQ_WD_LEFT_2 line18ZDFT_BZBZDQ_WD_LEFT_2 = new Line18ZDFT_BZBZDQ_WD_LEFT_2();
                                line18ZDFT_BZBZDQ_WD_LEFT_2.value = Math.Round(bzb_left2_wd_value * 0.1, 1);
                                line18ZDFT_BZBZDQ_WD_LEFT_2.ts = ts;
                                fTLine18ParamInfo.BZBZDQ_WD_LEFT_2 = line18ZDFT_BZBZDQ_WD_LEFT_2;

                                Line18ZDFT_BZBZDQ_WD_RIGHT_1 line18ZDFT_BZBZDQ_WD_RIGHT_1 = new Line18ZDFT_BZBZDQ_WD_RIGHT_1();
                                line18ZDFT_BZBZDQ_WD_RIGHT_1.value = Math.Round(bzb_right1_wd_value * 0.1, 1);
                                line18ZDFT_BZBZDQ_WD_RIGHT_1.ts = ts;
                                fTLine18ParamInfo.BZBZDQ_WD_RIGHT_1 = line18ZDFT_BZBZDQ_WD_RIGHT_1;

                                Line18ZDFT_BZBZDQ_WD_RIGHT_2 line18ZDFT_BZBZDQ_WD_RIGHT_2 = new Line18ZDFT_BZBZDQ_WD_RIGHT_2();
                                line18ZDFT_BZBZDQ_WD_RIGHT_2.value = Math.Round(bzb_right2_wd_value * 0.1, 1);
                                line18ZDFT_BZBZDQ_WD_RIGHT_2.ts = ts;
                                fTLine18ParamInfo.BZBZDQ_WD_RIGHT_2 = line18ZDFT_BZBZDQ_WD_RIGHT_2;

                                Line18ZDFT_TotalRunningTime line18ZDFT_TotalRunningTime2 = new Line18ZDFT_TotalRunningTime();
                                line18ZDFT_TotalRunningTime2.ts = ts;
                                line18ZDFT_TotalRunningTime2.value = time_hour;
                                fTLine18ParamInfo.TotalRunningTime = line18ZDFT_TotalRunningTime2;

                                Line18ZDFT_TotalRunningCounts line18ZDFT_TotalRunningCounts2 = new Line18ZDFT_TotalRunningCounts();
                                line18ZDFT_TotalRunningCounts2.ts = ts;
                                line18ZDFT_TotalRunningCounts2.value = num;
                                fTLine18ParamInfo.TotalRunningCounts = line18ZDFT_TotalRunningCounts2;

                                if (fTLine18ParamInfo.HJ_WD == null)
                                {
                                    Line18ZDFT_HJ_WD line18ZDFT_HJ_WD = new Line18ZDFT_HJ_WD();
                                    line18ZDFT_HJ_WD.ts = ts;
                                    fTLine18ParamInfo.HJ_WD = line18ZDFT_HJ_WD;

                                    Line18ZDFT_KZG_WD line18ZDFT_KZG_WD = new Line18ZDFT_KZG_WD();
                                    line18ZDFT_KZG_WD.ts = ts;
                                    fTLine18ParamInfo.KZG_WD = line18ZDFT_KZG_WD;

                                    Line18ZDFT_BPQ_WD line18ZDFT_BPQ_WD = new Line18ZDFT_BPQ_WD();
                                    line18ZDFT_BPQ_WD.ts = ts;
                                    fTLine18ParamInfo.BPQ_WD = line18ZDFT_BPQ_WD;

                                    Line18ZDFT_SBFZL line18ZDFT_SBFZL = new Line18ZDFT_SBFZL();
                                    line18ZDFT_SBFZL.ts = ts;
                                    fTLine18ParamInfo.SBFZL = line18ZDFT_SBFZL;

                                    Line18ZDFT_ZTJL line18ZDFT_ZTJL = new Line18ZDFT_ZTJL();
                                    line18ZDFT_ZTJL.ts = ts;
                                    fTLine18ParamInfo.ZTJL = line18ZDFT_ZTJL;

                                    Line18ZDFT_FSDSDPCL line18ZDFT_FSDSDPCL = new Line18ZDFT_FSDSDPCL();
                                    line18ZDFT_FSDSDPCL.ts = ts;
                                    fTLine18ParamInfo.FSDSDPCL = line18ZDFT_FSDSDPCL;

                                    Line18ZDFT_JSDCGQ1_QX_RMS line18ZDFT_JSDCGQ1_QX_RMS = new Line18ZDFT_JSDCGQ1_QX_RMS();
                                    line18ZDFT_JSDCGQ1_QX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_QX_RMS = line18ZDFT_JSDCGQ1_QX_RMS;

                                    Line18ZDFT_JSDCGQ1_HX_RMS line18ZDFT_JSDCGQ1_HX_RMS = new Line18ZDFT_JSDCGQ1_HX_RMS();
                                    line18ZDFT_JSDCGQ1_HX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_HX_RMS = line18ZDFT_JSDCGQ1_HX_RMS;

                                    Line18ZDFT_JSDCGQ1_CZ_RMS line18ZDFT_JSDCGQ1_CZ_RMS = new Line18ZDFT_JSDCGQ1_CZ_RMS();
                                    line18ZDFT_JSDCGQ1_CZ_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_CZ_RMS = line18ZDFT_JSDCGQ1_CZ_RMS;

                                    Line18ZDFT_JSDCGQ1_QX_PEAK line18ZDFT_JSDCGQ1_QX_PEAK = new Line18ZDFT_JSDCGQ1_QX_PEAK();
                                    line18ZDFT_JSDCGQ1_QX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_QX_PEAK = line18ZDFT_JSDCGQ1_QX_PEAK;

                                    Line18ZDFT_JSDCGQ1_HX_PEAK line18ZDFT_JSDCGQ1_HX_PEAK = new Line18ZDFT_JSDCGQ1_HX_PEAK();
                                    line18ZDFT_JSDCGQ1_HX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_HX_PEAK = line18ZDFT_JSDCGQ1_HX_PEAK;

                                    Line18ZDFT_JSDCGQ1_CZ_PEAK line18ZDFT_JSDCGQ1_CZ_PEAK = new Line18ZDFT_JSDCGQ1_CZ_PEAK();
                                    line18ZDFT_JSDCGQ1_CZ_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ1_CZ_PEAK = line18ZDFT_JSDCGQ1_CZ_PEAK;

                                    Line18ZDFT_JSDCGQ2_QX_RMS line18ZDFT_JSDCGQ2_QX_RMS = new Line18ZDFT_JSDCGQ2_QX_RMS();
                                    line18ZDFT_JSDCGQ2_QX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_QX_RMS = line18ZDFT_JSDCGQ2_QX_RMS;

                                    Line18ZDFT_JSDCGQ2_HX_RMS line18ZDFT_JSDCGQ2_HX_RMS = new Line18ZDFT_JSDCGQ2_HX_RMS();
                                    line18ZDFT_JSDCGQ2_HX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_HX_RMS = line18ZDFT_JSDCGQ2_HX_RMS;

                                    Line18ZDFT_JSDCGQ2_CZ_RMS line18ZDFT_JSDCGQ2_CZ_RMS = new Line18ZDFT_JSDCGQ2_CZ_RMS();
                                    line18ZDFT_JSDCGQ2_CZ_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_CZ_RMS = line18ZDFT_JSDCGQ2_CZ_RMS;

                                    Line18ZDFT_JSDCGQ2_QX_PEAK line18ZDFT_JSDCGQ2_QX_PEAK = new Line18ZDFT_JSDCGQ2_QX_PEAK();
                                    line18ZDFT_JSDCGQ2_QX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_QX_PEAK = line18ZDFT_JSDCGQ2_QX_PEAK;

                                    Line18ZDFT_JSDCGQ2_HX_PEAK line18ZDFT_JSDCGQ2_HX_PEAK = new Line18ZDFT_JSDCGQ2_HX_PEAK();
                                    line18ZDFT_JSDCGQ2_HX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_HX_PEAK = line18ZDFT_JSDCGQ2_HX_PEAK;

                                    Line18ZDFT_JSDCGQ2_CZ_PEAK line18ZDFT_JSDCGQ2_CZ_PEAK = new Line18ZDFT_JSDCGQ2_CZ_PEAK();
                                    line18ZDFT_JSDCGQ2_CZ_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ2_CZ_PEAK = line18ZDFT_JSDCGQ2_CZ_PEAK;

                                    Line18ZDFT_JSDCGQ3_QX_RMS line18ZDFT_JSDCGQ3_QX_RMS = new Line18ZDFT_JSDCGQ3_QX_RMS();
                                    line18ZDFT_JSDCGQ3_QX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_QX_RMS = line18ZDFT_JSDCGQ3_QX_RMS;

                                    Line18ZDFT_JSDCGQ3_HX_RMS line18ZDFT_JSDCGQ3_HX_RMS = new Line18ZDFT_JSDCGQ3_HX_RMS();
                                    line18ZDFT_JSDCGQ3_HX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_HX_RMS = line18ZDFT_JSDCGQ3_HX_RMS;

                                    Line18ZDFT_JSDCGQ3_CZ_RMS line18ZDFT_JSDCGQ3_CZ_RMS = new Line18ZDFT_JSDCGQ3_CZ_RMS();
                                    line18ZDFT_JSDCGQ3_CZ_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_CZ_RMS = line18ZDFT_JSDCGQ3_CZ_RMS;

                                    Line18ZDFT_JSDCGQ3_QX_PEAK line18ZDFT_JSDCGQ3_QX_PEAK = new Line18ZDFT_JSDCGQ3_QX_PEAK();
                                    line18ZDFT_JSDCGQ3_QX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_QX_PEAK = line18ZDFT_JSDCGQ3_QX_PEAK;

                                    Line18ZDFT_JSDCGQ3_HX_PEAK line18ZDFT_JSDCGQ3_HX_PEAK = new Line18ZDFT_JSDCGQ3_HX_PEAK();
                                    line18ZDFT_JSDCGQ3_HX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_HX_PEAK = line18ZDFT_JSDCGQ3_HX_PEAK;

                                    Line18ZDFT_JSDCGQ3_CZ_PEAK line18ZDFT_JSDCGQ3_CZ_PEAK = new Line18ZDFT_JSDCGQ3_CZ_PEAK();
                                    line18ZDFT_JSDCGQ3_CZ_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ3_CZ_PEAK = line18ZDFT_JSDCGQ3_CZ_PEAK;

                                    Line18ZDFT_JSDCGQ4_QX_RMS line18ZDFT_JSDCGQ4_QX_RMS = new Line18ZDFT_JSDCGQ4_QX_RMS();
                                    line18ZDFT_JSDCGQ4_QX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_QX_RMS = line18ZDFT_JSDCGQ4_QX_RMS;

                                    Line18ZDFT_JSDCGQ4_HX_RMS line18ZDFT_JSDCGQ4_HX_RMS = new Line18ZDFT_JSDCGQ4_HX_RMS();
                                    line18ZDFT_JSDCGQ4_HX_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_HX_RMS = line18ZDFT_JSDCGQ4_HX_RMS;

                                    Line18ZDFT_JSDCGQ4_CZ_RMS line18ZDFT_JSDCGQ4_CZ_RMS = new Line18ZDFT_JSDCGQ4_CZ_RMS();
                                    line18ZDFT_JSDCGQ4_CZ_RMS.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_CZ_RMS = line18ZDFT_JSDCGQ4_CZ_RMS;

                                    Line18ZDFT_JSDCGQ4_QX_PEAK line18ZDFT_JSDCGQ4_QX_PEAK = new Line18ZDFT_JSDCGQ4_QX_PEAK();
                                    line18ZDFT_JSDCGQ4_QX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_QX_PEAK = line18ZDFT_JSDCGQ4_QX_PEAK;

                                    Line18ZDFT_JSDCGQ4_HX_PEAK line18ZDFT_JSDCGQ4_HX_PEAK = new Line18ZDFT_JSDCGQ4_HX_PEAK();
                                    line18ZDFT_JSDCGQ4_HX_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_HX_PEAK = line18ZDFT_JSDCGQ4_HX_PEAK;

                                    Line18ZDFT_JSDCGQ4_CZ_PEAK line18ZDFT_JSDCGQ4_CZ_PEAK = new Line18ZDFT_JSDCGQ4_CZ_PEAK();
                                    line18ZDFT_JSDCGQ4_CZ_PEAK.ts = ts;
                                    fTLine18ParamInfo.JSDCGQ4_CZ_PEAK = line18ZDFT_JSDCGQ4_CZ_PEAK;

                                    Line18ZDFT_AlarmState line18ZDFT_AlarmState = new Line18ZDFT_AlarmState();
                                    line18ZDFT_AlarmState.ts = ts;
                                    fTLine18ParamInfo.AlarmState = line18ZDFT_AlarmState;

                                    Line18ZDFT_AlarmCode line18ZDFT_AlarmCode = new Line18ZDFT_AlarmCode();
                                    line18ZDFT_AlarmCode.ts = ts;
                                    fTLine18ParamInfo.AlarmCode = line18ZDFT_AlarmCode;

                                    Line18ZDFT_MotorVibrationSpeed line18ZDFT_MotorVibrationSpeed = new Line18ZDFT_MotorVibrationSpeed();
                                    line18ZDFT_MotorVibrationSpeed.ts = ts;
                                    fTLine18ParamInfo.motorVibrationSpeed = line18ZDFT_MotorVibrationSpeed;

                                    Line18ZDFT_GearboxVibrationSpeed line18ZDFT_GearboxVibrationSpeed = new Line18ZDFT_GearboxVibrationSpeed();
                                    line18ZDFT_GearboxVibrationSpeed.ts = ts;
                                    fTLine18ParamInfo.gearboxVibrationSpeed = line18ZDFT_GearboxVibrationSpeed;

                                    Line18ZDFT_BaseVibrationSpeed line18ZDFT_BaseVibrationSpeed = new Line18ZDFT_BaseVibrationSpeed();
                                    line18ZDFT_BaseVibrationSpeed.ts = ts;
                                    fTLine18ParamInfo.baseVibrationSpeed = line18ZDFT_BaseVibrationSpeed;

                                    Line18ZDFT_MainVibrationSpeed line18ZDFT_MainVibrationSpeed = new Line18ZDFT_MainVibrationSpeed();
                                    line18ZDFT_MainVibrationSpeed.ts = ts;
                                    fTLine18ParamInfo.mainVibrationSpeed = line18ZDFT_MainVibrationSpeed;

                                    Line18ZDFT_StepVibrationSpeed line18ZDFT_StepVibrationSpeed = new Line18ZDFT_StepVibrationSpeed();
                                    line18ZDFT_StepVibrationSpeed.ts = ts;
                                    fTLine18ParamInfo.stepVibrationSpeed = line18ZDFT_StepVibrationSpeed;
                                }


                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                byte[] bytes = new byte[4];
                                rng.GetBytes(bytes);
                                Line18DTDataPoint line18DTDataPoint = new Line18DTDataPoint();
                                line18DTDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();

                                //设备代码
                                Line18DataPointParamInfo line18DataPointParamInfo = new Line18DataPointParamInfo();
                                Line18ZDFT_REG_CODE line18ZDFT_REG_CODE = new Line18ZDFT_REG_CODE();
                                line18ZDFT_REG_CODE.value = DeviceCode;
                                line18ZDFT_REG_CODE.ts = ts;
                                line18DataPointParamInfo.REG_COD = line18ZDFT_REG_CODE;

                                //设备识别码
                                Line18ZDFT_identificationNumber line18ZDFT_IdentificationNumber = new Line18ZDFT_identificationNumber();
                                line18ZDFT_IdentificationNumber.value = SerialNumber;
                                line18ZDFT_IdentificationNumber.ts = ts;
                                line18DataPointParamInfo.identificationNumber = line18ZDFT_IdentificationNumber;

                                //数据产生时间
                                Line18ZDFT_timeStamps line18ZDFT_TimeStamps = new Line18ZDFT_timeStamps();
                                line18ZDFT_TimeStamps.value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                line18ZDFT_TimeStamps.ts = ts;
                                line18DataPointParamInfo.timeStamps = line18ZDFT_TimeStamps;

                                //服务模式
                                Line18ZDFT_serviceMode line18ZDFT_ServiceMode = new Line18ZDFT_serviceMode();
                                if (tz || gz || jt)
                                {
                                    line18ZDFT_ServiceMode.value = 0;
                                }
                                else if (yx)
                                {
                                    line18ZDFT_ServiceMode.value = 1;
                                }
                                else if (jx)
                                {
                                    line18ZDFT_ServiceMode.value = 2;
                                }
                                else
                                {
                                    line18ZDFT_ServiceMode.value = 3;
                                }
                                line18ZDFT_ServiceMode.ts = ts;
                                line18DataPointParamInfo.serviceMode = line18ZDFT_ServiceMode;

                                //运行状态
                                Line18ZDFT_operationStatus line18ZDFT_OperationStatus = new Line18ZDFT_operationStatus();
                                if (jt)
                                {
                                    line18ZDFT_OperationStatus.value = 2;
                                }
                                else if (tz || jx || gz || jt)
                                {
                                    line18ZDFT_OperationStatus.value = 0;
                                }
                                else
                                {
                                    line18ZDFT_OperationStatus.value = 1;
                                }
                                line18ZDFT_OperationStatus.ts = ts;
                                line18DataPointParamInfo.operationStatus = line18ZDFT_OperationStatus;

                                //运行方向
                                Line18ZDFT_operationDirection line18ZDFT_OperationDirection = new Line18ZDFT_operationDirection();
                                if (sx)
                                {
                                    line18ZDFT_OperationDirection.value = 1;
                                }
                                else if (xx)
                                {
                                    line18ZDFT_OperationDirection.value = 2;
                                }
                                else
                                {
                                    line18ZDFT_OperationDirection.value = 0;
                                }
                                line18ZDFT_OperationDirection.ts = ts;
                                line18DataPointParamInfo.operationDirection = line18ZDFT_OperationDirection;

                                //故障信号
                                Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ1 = new Line18ZDFT_GZ_BJ();
                                if (gz)
                                {
                                    line18ZDFT_GZ_BJ1.value = true;
                                }
                                else
                                {
                                    line18ZDFT_GZ_BJ1.value = false;
                                }
                                line18ZDFT_GZ_BJ1.ts = ts;
                                line18DataPointParamInfo.faultState = line18ZDFT_GZ_BJ1;

                                //故障代码
                                Line18ZDFT_FaultCode line18ZDFT_FaultCode = new Line18ZDFT_FaultCode();
                                line18ZDFT_FaultCode.value = Convert.ToInt32(code);
                                line18ZDFT_FaultCode.ts = ts;
                                line18DataPointParamInfo.faultCode = line18ZDFT_FaultCode;

                                //预警信号
                                Line18ZDFT_AlarmState line18ZDFT_AlarmState1 = new Line18ZDFT_AlarmState();
                                line18ZDFT_AlarmState1.value = false;
                                line18ZDFT_AlarmState1.ts = ts;
                                line18DataPointParamInfo.alarmState = line18ZDFT_AlarmState1;

                                //预警代码
                                Line18ZDFT_AlarmCode line18ZDFT_AlarmCode1 = new Line18ZDFT_AlarmCode();
                                line18ZDFT_AlarmCode1.value = 0;
                                line18ZDFT_AlarmCode1.ts = ts;
                                line18DataPointParamInfo.alarmCode = line18ZDFT_AlarmCode1;

                                //累计运行时间
                                Line18ZDFT_TotalRunningTime line18ZDFT_TotalRunningTime1 = new Line18ZDFT_TotalRunningTime();
                                line18ZDFT_TotalRunningTime1.value = time_hour;
                                line18ZDFT_TotalRunningTime1.ts = ts;
                                line18DataPointParamInfo.totalRunningTime = line18ZDFT_TotalRunningTime1;

                                //累计运行次数
                                Line18ZDFT_TotalRunningCounts line18ZDFT_TotalRunningCounts1 = new Line18ZDFT_TotalRunningCounts();
                                line18ZDFT_TotalRunningCounts1.value = num;
                                line18ZDFT_TotalRunningCounts1.ts = ts;
                                line18DataPointParamInfo.totalRunningCounts = line18ZDFT_TotalRunningCounts1;

                                //电机振动速度
                                Line18ZDFT_MotorVibrationSpeed line18ZDFT_MotorVibrationSpeed1 = new Line18ZDFT_MotorVibrationSpeed();
                                line18ZDFT_MotorVibrationSpeed1.value = 0;
                                line18ZDFT_MotorVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.motorVibrationSpeed = line18ZDFT_MotorVibrationSpeed1;
                                //减速机振动速度
                                Line18ZDFT_GearboxVibrationSpeed line18ZDFT_GearboxVibrationSpeed1 = new Line18ZDFT_GearboxVibrationSpeed();
                                line18ZDFT_GearboxVibrationSpeed1.value = 0;
                                line18ZDFT_GearboxVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.gearboxVibrationSpeed = line18ZDFT_GearboxVibrationSpeed1;
                                //底座振动速度
                                Line18ZDFT_BaseVibrationSpeed line18ZDFT_BaseVibrationSpeed1 = new Line18ZDFT_BaseVibrationSpeed();
                                line18ZDFT_BaseVibrationSpeed1.value = 0;
                                line18ZDFT_BaseVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.baseVibrationSpeed = line18ZDFT_BaseVibrationSpeed1;
                                //主驱动轮振动速度
                                Line18ZDFT_MainVibrationSpeed line18ZDFT_MainVibrationSpeed1 = new Line18ZDFT_MainVibrationSpeed();
                                line18ZDFT_MainVibrationSpeed1.value = 0;
                                line18ZDFT_MainVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.mainVibrationSpeed = line18ZDFT_MainVibrationSpeed1;
                                //梯级链涨紧轮振动速度
                                Line18ZDFT_StepVibrationSpeed line18ZDFT_StepVibrationSpeed1 = new Line18ZDFT_StepVibrationSpeed();
                                line18ZDFT_StepVibrationSpeed1.value = 0;
                                line18ZDFT_StepVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.stepVibrationSpeed = line18ZDFT_StepVibrationSpeed1;
                                line18DTDataPoint.ParamsInfo = line18DataPointParamInfo;

                                string message = JsonConvert.SerializeObject(line18DTDataPoint).Replace("ParamsInfo", "params");
                                if (mqttlog == 1)
                                {
                                    LogHelper.Log(message);
                                }
                                clientBUS.PublicMessage(topic, message, 0);
                                lastreceive = DateTime.Now;
                            }
                        }
                        else if (byteSerialPortRecvS0_1[i] == liftid && byteSerialPortRecvS0_1[i + 1] == 0x03 && byteSerialPortRecvS0_1[i + 2] == 0x46)
                        {
                            int numcount = Convert.ToInt32(byteSerialPortRecvS0_1[i + 2]);
                            byte[] buffers = new byte[numcount + 3];
                            for (int j = 0; j < buffers.Length; j++)
                            {
                                buffers[j] = byteSerialPortRecvS0_1[i + j];
                            }
                            byte[] crc = Crc18(buffers);
                            if (crc[0] == byteSerialPortRecvS0_1[i + numcount + 4] && crc[1] == byteSerialPortRecvS0_1[i + numcount + 3])
                            {
                                Array.Clear(byteSerialPortRecvS0_1, i, numcount + 5);

                                //解析桁架温度
                                byte[] hj_wd_data = new byte[2];
                                hj_wd_data[0] = buffers[4];
                                hj_wd_data[1] = buffers[3];
                                int hj_wd_value = GetTemperatureValue(hj_wd_data);

                                //解析控制柜温度
                                byte[] kzg_wd_data = new byte[2];
                                kzg_wd_data[0] = buffers[6];
                                kzg_wd_data[1] = buffers[5];
                                int kzg_wd_value = GetTemperatureValue(kzg_wd_data);

                                //解析变频器温度
                                byte[] bpq_wd_data = new byte[2];
                                bpq_wd_data[0] = buffers[8];
                                bpq_wd_data[1] = buffers[7];
                                int bpq_wd_value = GetTemperatureValue(bpq_wd_data);

                                //解析设备负载率
                                byte[] sbfzl_data = new byte[4];
                                sbfzl_data[0] = buffers[10];
                                sbfzl_data[1] = buffers[9];
                                sbfzl_data[2] = 0x00;
                                sbfzl_data[3] = 0x00;
                                int sbfzl_value = BitConverter.ToInt32(sbfzl_data);

                                //解析制停距离
                                byte[] ztjl_data = new byte[4];
                                ztjl_data[0] = buffers[12];
                                ztjl_data[1] = buffers[11];
                                ztjl_data[2] = 0x00;
                                ztjl_data[3] = 0x00;
                                int ztjl_value = BitConverter.ToInt32(ztjl_data);

                                //解析扶手带速度偏差率
                                byte[] fsdsdpcl_data = new byte[4];
                                fsdsdpcl_data[0] = buffers[14];
                                fsdsdpcl_data[1] = buffers[13];
                                fsdsdpcl_data[2] = 0x00;
                                fsdsdpcl_data[3] = 0x00;
                                int fsdsdpcl_value = BitConverter.ToInt32(fsdsdpcl_data);

                                //解析加速度传感器1前向RMS
                                byte[] jsdcgq1_qx_rms_data = new byte[4];
                                jsdcgq1_qx_rms_data[0] = buffers[26];
                                jsdcgq1_qx_rms_data[1] = buffers[25];
                                jsdcgq1_qx_rms_data[2] = 0x00;
                                jsdcgq1_qx_rms_data[3] = 0x00;
                                int jsdcgq1_qx_rms_value = BitConverter.ToInt32(jsdcgq1_qx_rms_data);

                                //解析加速度传感器1横向RMS
                                byte[] jsdcgq1_hx_rms_data = new byte[4];
                                jsdcgq1_hx_rms_data[0] = buffers[28];
                                jsdcgq1_hx_rms_data[1] = buffers[27];
                                jsdcgq1_hx_rms_data[2] = 0x00;
                                jsdcgq1_hx_rms_data[3] = 0x00;
                                int jsdcgq1_hx_rms_value = BitConverter.ToInt32(jsdcgq1_hx_rms_data);

                                //解析加速度传感器1垂直RMS
                                byte[] jsdcgq1_cz_rms_data = new byte[4];
                                jsdcgq1_cz_rms_data[0] = buffers[30];
                                jsdcgq1_cz_rms_data[1] = buffers[29];
                                jsdcgq1_cz_rms_data[2] = 0x00;
                                jsdcgq1_cz_rms_data[3] = 0x00;
                                int jsdcgq1_cz_rms_value = BitConverter.ToInt32(jsdcgq1_cz_rms_data);

                                //解析加速度传感器1前向PEAK
                                byte[] jsdcgq1_qx_peak_data = new byte[4];
                                jsdcgq1_qx_peak_data[0] = buffers[32];
                                jsdcgq1_qx_peak_data[1] = buffers[31];
                                jsdcgq1_qx_peak_data[2] = 0x00;
                                jsdcgq1_qx_peak_data[3] = 0x00;
                                int jsdcgq1_qx_peak_value = BitConverter.ToInt32(jsdcgq1_qx_peak_data);

                                //解析加速度传感器1横向PEAK
                                byte[] jsdcgq1_hx_peak_data = new byte[4];
                                jsdcgq1_hx_peak_data[0] = buffers[34];
                                jsdcgq1_hx_peak_data[1] = buffers[33];
                                jsdcgq1_hx_peak_data[2] = 0x00;
                                jsdcgq1_hx_peak_data[3] = 0x00;
                                int jsdcgq1_hx_peak_value = BitConverter.ToInt32(jsdcgq1_hx_peak_data);

                                //解析加速度传感器1垂直PEAK
                                byte[] jsdcgq1_cz_peak_data = new byte[4];
                                jsdcgq1_cz_peak_data[0] = buffers[36];
                                jsdcgq1_cz_peak_data[1] = buffers[35];
                                jsdcgq1_cz_peak_data[2] = 0x00;
                                jsdcgq1_cz_peak_data[3] = 0x00;
                                int jsdcgq1_cz_peak_value = BitConverter.ToInt32(jsdcgq1_cz_peak_data);

                                //解析加速度传感器2前向RMS
                                byte[] jsdcgq2_qx_rms_data = new byte[4];
                                jsdcgq2_qx_rms_data[0] = buffers[38];
                                jsdcgq2_qx_rms_data[1] = buffers[37];
                                jsdcgq2_qx_rms_data[2] = 0x00;
                                jsdcgq2_qx_rms_data[3] = 0x00;
                                int jsdcgq2_qx_rms_value = BitConverter.ToInt32(jsdcgq2_qx_rms_data);

                                //解析加速度传感器2横向RMS
                                byte[] jsdcgq2_hx_rms_data = new byte[4];
                                jsdcgq2_hx_rms_data[0] = buffers[40];
                                jsdcgq2_hx_rms_data[1] = buffers[39];
                                jsdcgq2_hx_rms_data[2] = 0x00;
                                jsdcgq2_hx_rms_data[3] = 0x00;
                                int jsdcgq2_hx_rms_value = BitConverter.ToInt32(jsdcgq2_hx_rms_data);

                                //解析加速度传感器2垂直RMS
                                byte[] jsdcgq2_cz_rms_data = new byte[4];
                                jsdcgq2_cz_rms_data[0] = buffers[42];
                                jsdcgq2_cz_rms_data[1] = buffers[41];
                                jsdcgq2_cz_rms_data[2] = 0x00;
                                jsdcgq2_cz_rms_data[3] = 0x00;
                                int jsdcgq2_cz_rms_value = BitConverter.ToInt32(jsdcgq2_cz_rms_data);

                                //解析加速度传感器2前向PEAK
                                byte[] jsdcgq2_qx_peak_data = new byte[4];
                                jsdcgq2_qx_peak_data[0] = buffers[44];
                                jsdcgq2_qx_peak_data[1] = buffers[43];
                                jsdcgq2_qx_peak_data[2] = 0x00;
                                jsdcgq2_qx_peak_data[3] = 0x00;
                                int jsdcgq2_qx_peak_value = BitConverter.ToInt32(jsdcgq2_qx_peak_data);

                                //解析加速度传感器2横向PEAK
                                byte[] jsdcgq2_hx_peak_data = new byte[4];
                                jsdcgq2_hx_peak_data[0] = buffers[46];
                                jsdcgq2_hx_peak_data[1] = buffers[45];
                                jsdcgq2_hx_peak_data[2] = 0x00;
                                jsdcgq2_hx_peak_data[3] = 0x00;
                                int jsdcgq2_hx_peak_value = BitConverter.ToInt32(jsdcgq2_hx_peak_data);

                                //解析加速度传感器2垂直PEAK
                                byte[] jsdcgq2_cz_peak_data = new byte[4];
                                jsdcgq2_cz_peak_data[0] = buffers[48];
                                jsdcgq2_cz_peak_data[1] = buffers[47];
                                jsdcgq2_cz_peak_data[2] = 0x00;
                                jsdcgq2_cz_peak_data[3] = 0x00;
                                int jsdcgq2_cz_peak_value = BitConverter.ToInt32(jsdcgq2_cz_peak_data);

                                //解析加速度传感器3前向RMS
                                byte[] jsdcgq3_qx_rms_data = new byte[4];
                                jsdcgq3_qx_rms_data[0] = buffers[50];
                                jsdcgq3_qx_rms_data[1] = buffers[49];
                                jsdcgq3_qx_rms_data[2] = 0x00;
                                jsdcgq3_qx_rms_data[3] = 0x00;
                                int jsdcgq3_qx_rms_value = BitConverter.ToInt32(jsdcgq3_qx_rms_data);

                                //解析加速度传感器3横向RMS
                                byte[] jsdcgq3_hx_rms_data = new byte[4];
                                jsdcgq3_hx_rms_data[0] = buffers[52];
                                jsdcgq3_hx_rms_data[1] = buffers[51];
                                jsdcgq3_hx_rms_data[2] = 0x00;
                                jsdcgq3_hx_rms_data[3] = 0x00;
                                int jsdcgq3_hx_rms_value = BitConverter.ToInt32(jsdcgq3_hx_rms_data);

                                //解析加速度传感器3垂直RMS
                                byte[] jsdcgq3_cz_rms_data = new byte[4];
                                jsdcgq3_cz_rms_data[0] = buffers[54];
                                jsdcgq3_cz_rms_data[1] = buffers[53];
                                jsdcgq3_cz_rms_data[2] = 0x00;
                                jsdcgq3_cz_rms_data[3] = 0x00;
                                int jsdcgq3_cz_rms_value = BitConverter.ToInt32(jsdcgq3_cz_rms_data);

                                //解析加速度传感器3前向PEAK
                                byte[] jsdcgq3_qx_peak_data = new byte[4];
                                jsdcgq3_qx_peak_data[0] = buffers[56];
                                jsdcgq3_qx_peak_data[1] = buffers[55];
                                jsdcgq3_qx_peak_data[2] = 0x00;
                                jsdcgq3_qx_peak_data[3] = 0x00;
                                int jsdcgq3_qx_peak_value = BitConverter.ToInt32(jsdcgq3_qx_peak_data);

                                //解析加速度传感器3横向PEAK
                                byte[] jsdcgq3_hx_peak_data = new byte[4];
                                jsdcgq3_hx_peak_data[0] = buffers[58];
                                jsdcgq3_hx_peak_data[1] = buffers[57];
                                jsdcgq3_hx_peak_data[2] = 0x00;
                                jsdcgq3_hx_peak_data[3] = 0x00;
                                int jsdcgq3_hx_peak_value = BitConverter.ToInt32(jsdcgq3_hx_peak_data);

                                //解析加速度传感器3垂直PEAK
                                byte[] jsdcgq3_cz_peak_data = new byte[4];
                                jsdcgq3_cz_peak_data[0] = buffers[60];
                                jsdcgq3_cz_peak_data[1] = buffers[59];
                                jsdcgq3_cz_peak_data[2] = 0x00;
                                jsdcgq3_cz_peak_data[3] = 0x00;
                                int jsdcgq3_cz_peak_value = BitConverter.ToInt32(jsdcgq3_cz_peak_data);

                                //解析加速度传感器4前向RMS
                                byte[] jsdcgq4_qx_rms_data = new byte[4];
                                jsdcgq4_qx_rms_data[0] = buffers[62];
                                jsdcgq4_qx_rms_data[1] = buffers[61];
                                jsdcgq4_qx_rms_data[2] = 0x00;
                                jsdcgq4_qx_rms_data[3] = 0x00;
                                int jsdcgq4_qx_rms_value = BitConverter.ToInt32(jsdcgq4_qx_rms_data);

                                //解析加速度传感器4横向RMS
                                byte[] jsdcgq4_hx_rms_data = new byte[4];
                                jsdcgq4_hx_rms_data[0] = buffers[64];
                                jsdcgq4_hx_rms_data[1] = buffers[63];
                                jsdcgq4_hx_rms_data[2] = 0x00;
                                jsdcgq4_hx_rms_data[3] = 0x00;
                                int jsdcgq4_hx_rms_value = BitConverter.ToInt32(jsdcgq4_hx_rms_data);

                                //解析加速度传感器4垂直RMS
                                byte[] jsdcgq4_cz_rms_data = new byte[4];
                                jsdcgq4_cz_rms_data[0] = buffers[66];
                                jsdcgq4_cz_rms_data[1] = buffers[65];
                                jsdcgq4_cz_rms_data[2] = 0x00;
                                jsdcgq4_cz_rms_data[3] = 0x00;
                                int jsdcgq4_cz_rms_value = BitConverter.ToInt32(jsdcgq4_cz_rms_data);

                                //解析加速度传感器4前向PEAK
                                byte[] jsdcgq4_qx_peak_data = new byte[4];
                                jsdcgq4_qx_peak_data[0] = buffers[68];
                                jsdcgq4_qx_peak_data[1] = buffers[67];
                                jsdcgq4_qx_peak_data[2] = 0x00;
                                jsdcgq4_qx_peak_data[3] = 0x00;
                                int jsdcgq4_qx_peak_value = BitConverter.ToInt32(jsdcgq4_qx_peak_data);

                                //解析加速度传感器4横向PEAK
                                byte[] jsdcgq4_hx_peak_data = new byte[4];
                                jsdcgq4_hx_peak_data[0] = buffers[70];
                                jsdcgq4_hx_peak_data[1] = buffers[69];
                                jsdcgq4_hx_peak_data[2] = 0x00;
                                jsdcgq4_hx_peak_data[3] = 0x00;
                                int jsdcgq4_hx_peak_value = BitConverter.ToInt32(jsdcgq4_hx_peak_data);

                                //解析加速度传感器4垂直PEAK
                                byte[] jsdcgq4_cz_peak_data = new byte[4];
                                jsdcgq4_cz_peak_data[0] = buffers[72];
                                jsdcgq4_cz_peak_data[1] = buffers[71];
                                jsdcgq4_cz_peak_data[2] = 0x00;
                                jsdcgq4_cz_peak_data[3] = 0x00;
                                int jsdcgq4_cz_peak_value = BitConverter.ToInt32(jsdcgq4_cz_peak_data);

                                long ts = DateTimeOffset.Now.ToUnixTimeSeconds();

                                Line18ZDFT_TX_BJ line18ZDFT_TX_BJ = new Line18ZDFT_TX_BJ();
                                line18ZDFT_TX_BJ.ts = ts;
                                line18ZDFT_TX_BJ.value = true;
                                fTLine18ParamInfo.ZDFT_TX_BJ = line18ZDFT_TX_BJ;

                                Line18ZDFT_HJ_WD line18ZDFT_HJ_WD = new Line18ZDFT_HJ_WD();
                                line18ZDFT_HJ_WD.value = Math.Round(hj_wd_value * 0.1, 1);
                                line18ZDFT_HJ_WD.ts = ts;
                                fTLine18ParamInfo.HJ_WD = line18ZDFT_HJ_WD;

                                Line18ZDFT_KZG_WD line18ZDFT_KZG_WD = new Line18ZDFT_KZG_WD();
                                line18ZDFT_KZG_WD.value = Math.Round(kzg_wd_value * 0.1, 1);
                                line18ZDFT_KZG_WD.ts = ts;
                                fTLine18ParamInfo.KZG_WD = line18ZDFT_KZG_WD;

                                Line18ZDFT_BPQ_WD line18ZDFT_BPQ_WD = new Line18ZDFT_BPQ_WD();
                                line18ZDFT_BPQ_WD.value = Math.Round(bpq_wd_value * 0.1, 1);
                                line18ZDFT_BPQ_WD.ts = ts;
                                fTLine18ParamInfo.BPQ_WD = line18ZDFT_BPQ_WD;

                                Line18ZDFT_SBFZL line18ZDFT_SBFZL = new Line18ZDFT_SBFZL();
                                line18ZDFT_SBFZL.value = Math.Round(sbfzl_value * 0.0001, 4);
                                line18ZDFT_SBFZL.ts = ts;
                                fTLine18ParamInfo.SBFZL = line18ZDFT_SBFZL;

                                Line18ZDFT_ZTJL line18ZDFT_ZTJL = new Line18ZDFT_ZTJL();
                                line18ZDFT_ZTJL.value = ztjl_value;
                                line18ZDFT_ZTJL.ts = ts;
                                fTLine18ParamInfo.ZTJL = line18ZDFT_ZTJL;

                                Line18ZDFT_FSDSDPCL line18ZDFT_FSDSDPCL = new Line18ZDFT_FSDSDPCL();
                                line18ZDFT_FSDSDPCL.value = Math.Round(fsdsdpcl_value * 0.0001, 4);
                                line18ZDFT_FSDSDPCL.ts = ts;
                                fTLine18ParamInfo.FSDSDPCL = line18ZDFT_FSDSDPCL;

                                Line18ZDFT_JSDCGQ1_QX_RMS line18ZDFT_JSDCGQ1_QX_RMS = new Line18ZDFT_JSDCGQ1_QX_RMS();
                                line18ZDFT_JSDCGQ1_QX_RMS.value = jsdcgq1_qx_rms_value;
                                line18ZDFT_JSDCGQ1_QX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_QX_RMS = line18ZDFT_JSDCGQ1_QX_RMS;

                                Line18ZDFT_JSDCGQ1_HX_RMS line18ZDFT_JSDCGQ1_HX_RMS = new Line18ZDFT_JSDCGQ1_HX_RMS();
                                line18ZDFT_JSDCGQ1_HX_RMS.value = jsdcgq1_hx_rms_value;
                                line18ZDFT_JSDCGQ1_HX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_HX_RMS = line18ZDFT_JSDCGQ1_HX_RMS;

                                Line18ZDFT_JSDCGQ1_CZ_RMS line18ZDFT_JSDCGQ1_CZ_RMS = new Line18ZDFT_JSDCGQ1_CZ_RMS();
                                line18ZDFT_JSDCGQ1_CZ_RMS.value = jsdcgq1_cz_rms_value;
                                line18ZDFT_JSDCGQ1_CZ_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_CZ_RMS = line18ZDFT_JSDCGQ1_CZ_RMS;

                                Line18ZDFT_JSDCGQ1_QX_PEAK line18ZDFT_JSDCGQ1_QX_PEAK = new Line18ZDFT_JSDCGQ1_QX_PEAK();
                                line18ZDFT_JSDCGQ1_QX_PEAK.value = jsdcgq1_qx_peak_value;
                                line18ZDFT_JSDCGQ1_QX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_QX_PEAK = line18ZDFT_JSDCGQ1_QX_PEAK;

                                Line18ZDFT_JSDCGQ1_HX_PEAK line18ZDFT_JSDCGQ1_HX_PEAK = new Line18ZDFT_JSDCGQ1_HX_PEAK();
                                line18ZDFT_JSDCGQ1_HX_PEAK.value = jsdcgq1_hx_peak_value;
                                line18ZDFT_JSDCGQ1_HX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_HX_PEAK = line18ZDFT_JSDCGQ1_HX_PEAK;

                                Line18ZDFT_JSDCGQ1_CZ_PEAK line18ZDFT_JSDCGQ1_CZ_PEAK = new Line18ZDFT_JSDCGQ1_CZ_PEAK();
                                line18ZDFT_JSDCGQ1_CZ_PEAK.value = jsdcgq1_cz_peak_value;
                                line18ZDFT_JSDCGQ1_CZ_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ1_CZ_PEAK = line18ZDFT_JSDCGQ1_CZ_PEAK;

                                Line18ZDFT_JSDCGQ2_QX_RMS line18ZDFT_JSDCGQ2_QX_RMS = new Line18ZDFT_JSDCGQ2_QX_RMS();
                                line18ZDFT_JSDCGQ2_QX_RMS.value = jsdcgq2_qx_rms_value;
                                line18ZDFT_JSDCGQ2_QX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_QX_RMS = line18ZDFT_JSDCGQ2_QX_RMS;

                                Line18ZDFT_JSDCGQ2_HX_RMS line18ZDFT_JSDCGQ2_HX_RMS = new Line18ZDFT_JSDCGQ2_HX_RMS();
                                line18ZDFT_JSDCGQ2_HX_RMS.value = jsdcgq2_hx_rms_value;
                                line18ZDFT_JSDCGQ2_HX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_HX_RMS = line18ZDFT_JSDCGQ2_HX_RMS;

                                Line18ZDFT_JSDCGQ2_CZ_RMS line18ZDFT_JSDCGQ2_CZ_RMS = new Line18ZDFT_JSDCGQ2_CZ_RMS();
                                line18ZDFT_JSDCGQ2_CZ_RMS.value = jsdcgq2_cz_rms_value;
                                line18ZDFT_JSDCGQ2_CZ_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_CZ_RMS = line18ZDFT_JSDCGQ2_CZ_RMS;

                                Line18ZDFT_JSDCGQ2_QX_PEAK line18ZDFT_JSDCGQ2_QX_PEAK = new Line18ZDFT_JSDCGQ2_QX_PEAK();
                                line18ZDFT_JSDCGQ2_QX_PEAK.value = jsdcgq2_qx_peak_value;
                                line18ZDFT_JSDCGQ2_QX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_QX_PEAK = line18ZDFT_JSDCGQ2_QX_PEAK;

                                Line18ZDFT_JSDCGQ2_HX_PEAK line18ZDFT_JSDCGQ2_HX_PEAK = new Line18ZDFT_JSDCGQ2_HX_PEAK();
                                line18ZDFT_JSDCGQ2_HX_PEAK.value = jsdcgq2_hx_peak_value;
                                line18ZDFT_JSDCGQ2_HX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_HX_PEAK = line18ZDFT_JSDCGQ2_HX_PEAK;

                                Line18ZDFT_JSDCGQ2_CZ_PEAK line18ZDFT_JSDCGQ2_CZ_PEAK = new Line18ZDFT_JSDCGQ2_CZ_PEAK();
                                line18ZDFT_JSDCGQ2_CZ_PEAK.value = jsdcgq2_cz_peak_value;
                                line18ZDFT_JSDCGQ2_CZ_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ2_CZ_PEAK = line18ZDFT_JSDCGQ2_CZ_PEAK;

                                Line18ZDFT_JSDCGQ3_QX_RMS line18ZDFT_JSDCGQ3_QX_RMS = new Line18ZDFT_JSDCGQ3_QX_RMS();
                                line18ZDFT_JSDCGQ3_QX_RMS.value = jsdcgq3_qx_rms_value;
                                line18ZDFT_JSDCGQ3_QX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_QX_RMS = line18ZDFT_JSDCGQ3_QX_RMS;

                                Line18ZDFT_JSDCGQ3_HX_RMS line18ZDFT_JSDCGQ3_HX_RMS = new Line18ZDFT_JSDCGQ3_HX_RMS();
                                line18ZDFT_JSDCGQ3_HX_RMS.value = jsdcgq3_hx_rms_value;
                                line18ZDFT_JSDCGQ3_HX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_HX_RMS = line18ZDFT_JSDCGQ3_HX_RMS;

                                Line18ZDFT_JSDCGQ3_CZ_RMS line18ZDFT_JSDCGQ3_CZ_RMS = new Line18ZDFT_JSDCGQ3_CZ_RMS();
                                line18ZDFT_JSDCGQ3_CZ_RMS.value = jsdcgq3_cz_rms_value;
                                line18ZDFT_JSDCGQ3_CZ_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_CZ_RMS = line18ZDFT_JSDCGQ3_CZ_RMS;

                                Line18ZDFT_JSDCGQ3_QX_PEAK line18ZDFT_JSDCGQ3_QX_PEAK = new Line18ZDFT_JSDCGQ3_QX_PEAK();
                                line18ZDFT_JSDCGQ3_QX_PEAK.value = jsdcgq3_qx_peak_value;
                                line18ZDFT_JSDCGQ3_QX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_QX_PEAK = line18ZDFT_JSDCGQ3_QX_PEAK;

                                Line18ZDFT_JSDCGQ3_HX_PEAK line18ZDFT_JSDCGQ3_HX_PEAK = new Line18ZDFT_JSDCGQ3_HX_PEAK();
                                line18ZDFT_JSDCGQ3_HX_PEAK.value = jsdcgq3_hx_peak_value;
                                line18ZDFT_JSDCGQ3_HX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_HX_PEAK = line18ZDFT_JSDCGQ3_HX_PEAK;

                                Line18ZDFT_JSDCGQ3_CZ_PEAK line18ZDFT_JSDCGQ3_CZ_PEAK = new Line18ZDFT_JSDCGQ3_CZ_PEAK();
                                line18ZDFT_JSDCGQ3_CZ_PEAK.value = jsdcgq3_cz_peak_value;
                                line18ZDFT_JSDCGQ3_CZ_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ3_CZ_PEAK = line18ZDFT_JSDCGQ3_CZ_PEAK;

                                Line18ZDFT_JSDCGQ4_QX_RMS line18ZDFT_JSDCGQ4_QX_RMS = new Line18ZDFT_JSDCGQ4_QX_RMS();
                                line18ZDFT_JSDCGQ4_QX_RMS.value = jsdcgq4_qx_rms_value;
                                line18ZDFT_JSDCGQ4_QX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_QX_RMS = line18ZDFT_JSDCGQ4_QX_RMS;

                                Line18ZDFT_JSDCGQ4_HX_RMS line18ZDFT_JSDCGQ4_HX_RMS = new Line18ZDFT_JSDCGQ4_HX_RMS();
                                line18ZDFT_JSDCGQ4_HX_RMS.value = jsdcgq4_hx_rms_value;
                                line18ZDFT_JSDCGQ4_HX_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_HX_RMS = line18ZDFT_JSDCGQ4_HX_RMS;

                                Line18ZDFT_JSDCGQ4_CZ_RMS line18ZDFT_JSDCGQ4_CZ_RMS = new Line18ZDFT_JSDCGQ4_CZ_RMS();
                                line18ZDFT_JSDCGQ4_CZ_RMS.value = jsdcgq4_cz_rms_value;
                                line18ZDFT_JSDCGQ4_CZ_RMS.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_CZ_RMS = line18ZDFT_JSDCGQ4_CZ_RMS;

                                Line18ZDFT_JSDCGQ4_QX_PEAK line18ZDFT_JSDCGQ4_QX_PEAK = new Line18ZDFT_JSDCGQ4_QX_PEAK();
                                line18ZDFT_JSDCGQ4_QX_PEAK.value = jsdcgq4_qx_peak_value;
                                line18ZDFT_JSDCGQ4_QX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_QX_PEAK = line18ZDFT_JSDCGQ4_QX_PEAK;

                                Line18ZDFT_JSDCGQ4_HX_PEAK line18ZDFT_JSDCGQ4_HX_PEAK = new Line18ZDFT_JSDCGQ4_HX_PEAK();
                                line18ZDFT_JSDCGQ4_HX_PEAK.value = jsdcgq4_hx_peak_value;
                                line18ZDFT_JSDCGQ4_HX_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_HX_PEAK = line18ZDFT_JSDCGQ4_HX_PEAK;

                                Line18ZDFT_JSDCGQ4_CZ_PEAK line18ZDFT_JSDCGQ4_CZ_PEAK = new Line18ZDFT_JSDCGQ4_CZ_PEAK();
                                line18ZDFT_JSDCGQ4_CZ_PEAK.value = jsdcgq4_cz_peak_value;
                                line18ZDFT_JSDCGQ4_CZ_PEAK.ts = ts;
                                fTLine18ParamInfo.JSDCGQ4_CZ_PEAK = line18ZDFT_JSDCGQ4_CZ_PEAK;

                                if (fTLine18ParamInfo.ZDFT_TX_BJ == null)
                                {
                                    Line18ZDFT_SX_ZT line18ZDFT_SX_ZT = new Line18ZDFT_SX_ZT();
                                    line18ZDFT_SX_ZT.ts = ts;
                                    line18ZDFT_SX_ZT.value = false;
                                    fTLine18ParamInfo.ZDFT_SX_ZT = line18ZDFT_SX_ZT;

                                    Line18ZDFT_XX_ZT line18ZDFT_XX_ = new Line18ZDFT_XX_ZT();
                                    line18ZDFT_XX_.ts = ts;
                                    line18ZDFT_XX_.value = false;
                                    fTLine18ParamInfo.ZDFT_XX_ZT = line18ZDFT_XX_;

                                    Line18ZDFT_YX_ZT line18ZDFT_YX_ZT = new Line18ZDFT_YX_ZT();
                                    line18ZDFT_YX_ZT.ts = ts;
                                    line18ZDFT_YX_ZT.value = false;
                                    fTLine18ParamInfo.ZDFT_YX_ZT = line18ZDFT_YX_ZT;

                                    Line18ZDFT_TZ_ZT line18ZDFT_TZ_ZT = new Line18ZDFT_TZ_ZT();
                                    line18ZDFT_TZ_ZT.ts = ts;
                                    line18ZDFT_TZ_ZT.value = false;
                                    fTLine18ParamInfo.ZDFT_TZ_ZT = line18ZDFT_TZ_ZT;

                                    Line18ZDFT_JX_BJ line18ZDFT_JX_BJ = new Line18ZDFT_JX_BJ();
                                    line18ZDFT_JX_BJ.ts = ts;
                                    line18ZDFT_JX_BJ.value = false;
                                    fTLine18ParamInfo.ZDFT_JX_BJ = line18ZDFT_JX_BJ;

                                    Line18ZDFT_GZ_DM line18ZDFT_GZ_DM = new Line18ZDFT_GZ_DM();
                                    line18ZDFT_GZ_DM.ts = ts;
                                    line18ZDFT_GZ_DM.value = "";
                                    fTLine18ParamInfo.ZDFT_GZ_DM = line18ZDFT_GZ_DM;

                                    Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ = new Line18ZDFT_GZ_BJ();
                                    line18ZDFT_GZ_BJ.ts = ts;
                                    line18ZDFT_GZ_BJ.value = false;
                                    fTLine18ParamInfo.ZDFT_GZ_BJ = line18ZDFT_GZ_BJ;

                                    Line18ZDFT_JT_BJ line18ZDFT_JT_BJ = new Line18ZDFT_JT_BJ();
                                    line18ZDFT_JT_BJ.ts = ts;
                                    line18ZDFT_JT_BJ.value = false;
                                    fTLine18ParamInfo.ZDFT_JT_BJ = line18ZDFT_JT_BJ;

                                    Line18ZDFT_Y_MS line18ZDFT_Y_MS = new Line18ZDFT_Y_MS();
                                    line18ZDFT_Y_MS.ts = ts;
                                    line18ZDFT_Y_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_Y_MS = line18ZDFT_Y_MS;

                                    Line18ZDFT_JNYX_MS line18ZDFT_JNYX_MS = new Line18ZDFT_JNYX_MS();
                                    line18ZDFT_JNYX_MS.ts = ts;
                                    line18ZDFT_JNYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_JNYX_MS = line18ZDFT_JNYX_MS;

                                    Line18ZDFT_DSYX_MS line18ZDFT_DSYX_MS = new Line18ZDFT_DSYX_MS();
                                    line18ZDFT_DSYX_MS.ts = ts;
                                    line18ZDFT_DSYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_DSYX_MS = line18ZDFT_DSYX_MS;

                                    Line18ZDFT_GSYX_MS line18ZDFT_GSYX_MS = new Line18ZDFT_GSYX_MS();
                                    line18ZDFT_GSYX_MS.ts = ts;
                                    line18ZDFT_GSYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_GSYX_MS = line18ZDFT_GSYX_MS;

                                    Line18ZDFT_QSYX_MS line18ZDFT_QSYX_MS = new Line18ZDFT_QSYX_MS();
                                    line18ZDFT_QSYX_MS.ts = ts;
                                    line18ZDFT_QSYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_QSYX_MS = line18ZDFT_QSYX_MS;

                                    Line18ZDFT_BP_MS line18ZDFT_BP_MS = new Line18ZDFT_BP_MS();
                                    line18ZDFT_BP_MS.ts = ts;
                                    line18ZDFT_BP_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_BP_MS = line18ZDFT_BP_MS;

                                    Line18ZDFT_ZDYX_MS line18ZDFT_ZDYX_MS = new Line18ZDFT_ZDYX_MS();
                                    line18ZDFT_ZDYX_MS.ts = ts;
                                    line18ZDFT_ZDYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_ZDYX_MS = line18ZDFT_ZDYX_MS;

                                    Line18ZDFT_SXYX_MS line18ZDFT_SXYX_MS = new Line18ZDFT_SXYX_MS();
                                    line18ZDFT_SXYX_MS.ts = ts;
                                    line18ZDFT_SXYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_SXYX_MS = line18ZDFT_SXYX_MS;

                                    Line18ZDFT_LXYX_MS line18ZDFT_LXYX_MS = new Line18ZDFT_LXYX_MS();
                                    line18ZDFT_LXYX_MS.ts = ts;
                                    line18ZDFT_LXYX_MS.value = false;
                                    fTLine18ParamInfo.ZDFT_LXYX_MS = line18ZDFT_LXYX_MS;

                                    Line18ZDFT_BZBZDQ_WD_LEFT_1 line18ZDFT_BZBZDQ_WD_LEFT_1 = new Line18ZDFT_BZBZDQ_WD_LEFT_1();
                                    line18ZDFT_BZBZDQ_WD_LEFT_1.ts = ts;
                                    fTLine18ParamInfo.BZBZDQ_WD_LEFT_1 = line18ZDFT_BZBZDQ_WD_LEFT_1;

                                    Line18ZDFT_BZBZDQ_WD_LEFT_2 line18ZDFT_BZBZDQ_WD_LEFT_2 = new Line18ZDFT_BZBZDQ_WD_LEFT_2();
                                    line18ZDFT_BZBZDQ_WD_LEFT_2.ts = ts;
                                    fTLine18ParamInfo.BZBZDQ_WD_LEFT_2 = line18ZDFT_BZBZDQ_WD_LEFT_2;

                                    Line18ZDFT_BZBZDQ_WD_RIGHT_1 line18ZDFT_BZBZDQ_WD_RIGHT_1 = new Line18ZDFT_BZBZDQ_WD_RIGHT_1();
                                    line18ZDFT_BZBZDQ_WD_RIGHT_1.ts = ts;
                                    fTLine18ParamInfo.BZBZDQ_WD_RIGHT_1 = line18ZDFT_BZBZDQ_WD_RIGHT_1;

                                    Line18ZDFT_BZBZDQ_WD_RIGHT_2 line18ZDFT_BZBZDQ_WD_RIGHT_2 = new Line18ZDFT_BZBZDQ_WD_RIGHT_2();
                                    line18ZDFT_BZBZDQ_WD_RIGHT_2.ts = ts;
                                    fTLine18ParamInfo.BZBZDQ_WD_RIGHT_2 = line18ZDFT_BZBZDQ_WD_RIGHT_2;
                                }

                                Line18DataPoint line18DataPoint = new Line18DataPoint();
                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                byte[] bytes = new byte[4];
                                rng.GetBytes(bytes);
                                line18DataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                line18DataPoint.ParamsInfo = fTLine18ParamInfo;
                                line18DataPoint.SerialNumber = SerialNumber;
                                line18DataPoint.DeviceCode = DeviceCode;
                                string message = JsonConvert.SerializeObject(line18DataPoint).Replace("ParamsInfo", "params");
                                //if (mqttlog == 1)
                                //{
                                //    LogHelper.Log(message);
                                //}
                                //clientBUS.PublicMessage(topic, message, 0);
                                lastreceive = DateTime.Now;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            SerialPortRecvTimerS0_1.Start();
        }

        public int GetTemperatureValue(byte[] data)
        {
            int temperature = 0;
            try
            {
                BitArray wd_bits;
                wd_bits = new BitArray(data);

                int bzb_left1_wd_value = 0;
                bool wd_flag = wd_bits.Get(15);
                byte[] wd_value = new byte[4];
                wd_value[0] = data[0];
                wd_value[1] = data[1];
                wd_value[2] = 0x00;
                wd_value[3] = 0x00;
                bzb_left1_wd_value = BitConverter.ToInt32(wd_value, 0);
                if (wd_flag)
                {
                    temperature = ToTwosComplement(bzb_left1_wd_value * -1, 8) * -1;
                }
                else
                {
                    temperature = bzb_left1_wd_value;
                }
            }
            catch (Exception ex)
            {

            }
            return temperature;
        }

        public int ToTwosComplement(int number, int bitCount)
        {
            int mask = (int)((1 << bitCount) - 1);
            return (int)number & mask;
        }

        // byte转字符串
        public static string ToHexs(byte[] data)
        {
            if (data.Length <= 0) return "";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("X2") + " ");
            }
            string str = sb.ToString();
            return str;
        }

        #endregion

        #region 查询扶梯状态
        public static CancellationTokenSource cmd_tokenSource = new CancellationTokenSource();
        public CancellationToken cmd_token = cmd_tokenSource.Token;
        public ManualResetEvent cmd_resetEvent = new ManualResetEvent(true);
        public DateTime lastreceive = DateTime.Now;
        public DateTime Simulation_lastreceive = DateTime.Now;
        public int fault_code = 0;

        public int liftid;
        public void QueryElevatorStatus()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        if (cmds.Count > 0)
                        {
                            if (cmds[0].type == 1)
                            {
                                QueryttyS1_Status(cmds[0].activateCmd);
                                Task.Delay(200).Wait();
                            }
                            QueryttyS1_Status(cmds[0].cmd);
                            cmds.RemoveAt(0);
                        }
                        else
                        {
                            QueryttyS1_Status(query_cmd);
                            Task.Delay(200).Wait();
                            QueryttyS1_Status(query_cmd_2);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("定时发送异常" + ex.ToString());
                    }

                    Task.Delay(1000).Wait();
                }
            });
        }

        public void QueryMqttStatus()
        {
            Task t = Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    try
                    {
                        if (Simulation == 1)
                        {
                            TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - Simulation_lastreceive.Ticks);
                            if (secondSpan.TotalMilliseconds > Interval)
                            {
                                long ts = DateTimeOffset.Now.ToUnixTimeSeconds();

                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                byte[] bytes = new byte[4];
                                rng.GetBytes(bytes);
                                Line18DTDataPoint line18DTDataPoint = new Line18DTDataPoint();
                                line18DTDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();

                                //设备代码
                                Line18DataPointParamInfo line18DataPointParamInfo = new Line18DataPointParamInfo();
                                Line18ZDFT_REG_CODE line18ZDFT_REG_CODE = new Line18ZDFT_REG_CODE();
                                line18ZDFT_REG_CODE.value = DeviceCode;
                                line18ZDFT_REG_CODE.ts = ts;
                                line18DataPointParamInfo.REG_COD = line18ZDFT_REG_CODE;

                                //设备识别码
                                Line18ZDFT_identificationNumber line18ZDFT_IdentificationNumber = new Line18ZDFT_identificationNumber();
                                line18ZDFT_IdentificationNumber.value = SerialNumber;
                                line18ZDFT_IdentificationNumber.ts = ts;
                                line18DataPointParamInfo.identificationNumber = line18ZDFT_IdentificationNumber;

                                //数据产生时间
                                Line18ZDFT_timeStamps line18ZDFT_TimeStamps = new Line18ZDFT_timeStamps();
                                line18ZDFT_TimeStamps.value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                line18ZDFT_TimeStamps.ts = ts;
                                line18DataPointParamInfo.timeStamps = line18ZDFT_TimeStamps;

                                //服务模式
                                Line18ZDFT_serviceMode line18ZDFT_ServiceMode = new Line18ZDFT_serviceMode();
                                if (Line18State.fx == 2 || Line18State.gz || Line18State.jt)
                                {
                                    line18ZDFT_ServiceMode.value = 0;
                                }
                                else if (Line18State.yx)
                                {
                                    line18ZDFT_ServiceMode.value = 1;
                                }
                                else if (Line18State.jx)
                                {
                                    line18ZDFT_ServiceMode.value = 2;
                                }
                                else
                                {
                                    line18ZDFT_ServiceMode.value = 3;
                                }
                                line18ZDFT_ServiceMode.ts = ts;
                                line18DataPointParamInfo.serviceMode = line18ZDFT_ServiceMode;

                                //运行状态
                                Line18ZDFT_operationStatus line18ZDFT_OperationStatus = new Line18ZDFT_operationStatus();
                                if (Line18State.jt)
                                {
                                    line18ZDFT_OperationStatus.value = 2;
                                }
                                else if (Line18State.fx == 2 || Line18State.jx || Line18State.gz)
                                {
                                    line18ZDFT_OperationStatus.value = 0;
                                }
                                else
                                {
                                    line18ZDFT_OperationStatus.value = 1;
                                }
                                line18ZDFT_OperationStatus.ts = ts;
                                line18DataPointParamInfo.operationStatus = line18ZDFT_OperationStatus;

                                //运行方向
                                Line18ZDFT_operationDirection line18ZDFT_OperationDirection = new Line18ZDFT_operationDirection();
                                if (Line18State.fx == 0)
                                {
                                    line18ZDFT_OperationDirection.value = 1;
                                }
                                else if (Line18State.fx == 1)
                                {
                                    line18ZDFT_OperationDirection.value = 2;
                                }
                                else
                                {
                                    line18ZDFT_OperationDirection.value = 0;
                                }
                                line18ZDFT_OperationDirection.ts = ts;
                                line18DataPointParamInfo.operationDirection = line18ZDFT_OperationDirection;

                                //故障
                                Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ1 = new Line18ZDFT_GZ_BJ();
                                if (Line18State.gz)
                                {
                                    line18ZDFT_GZ_BJ1.value = true;
                                }
                                else
                                {
                                    line18ZDFT_GZ_BJ1.value = false;
                                }
                                line18ZDFT_GZ_BJ1.ts = ts;
                                line18DataPointParamInfo.faultState = line18ZDFT_GZ_BJ1;

                                //故障代码
                                Line18ZDFT_FaultCode line18ZDFT_FaultCode = new Line18ZDFT_FaultCode();
                                if (string.IsNullOrEmpty(Line18State.gzdm))
                                {
                                    line18ZDFT_FaultCode.value = 0;
                                }
                                else
                                {
                                    line18ZDFT_FaultCode.value = Convert.ToInt32(Line18State.gzdm);
                                }
                                line18ZDFT_FaultCode.ts = ts;
                                line18DataPointParamInfo.faultCode = line18ZDFT_FaultCode;

                                //预警
                                Line18ZDFT_AlarmState line18ZDFT_AlarmState1 = new Line18ZDFT_AlarmState();
                                if (Line18State.alarmState)
                                {
                                    line18ZDFT_AlarmState1.value = true;
                                }
                                else
                                {
                                    line18ZDFT_AlarmState1.value = false;
                                }
                                line18ZDFT_AlarmState1.ts = ts;
                                line18DataPointParamInfo.alarmState = line18ZDFT_AlarmState1;

                                //预警代码
                                Line18ZDFT_AlarmCode line18ZDFT_AlarmCode1 = new Line18ZDFT_AlarmCode();
                                line18ZDFT_AlarmCode1.value = Line18State.alarmCode;
                                line18ZDFT_AlarmCode1.ts = ts;
                                line18DataPointParamInfo.alarmCode = line18ZDFT_AlarmCode1;

                                //累计运行时间
                                Line18ZDFT_TotalRunningTime line18ZDFT_TotalRunningTime1 = new Line18ZDFT_TotalRunningTime();
                                line18ZDFT_TotalRunningTime1.value = Line18State.totalRunningTime;
                                line18ZDFT_TotalRunningTime1.ts = ts;
                                line18DataPointParamInfo.totalRunningTime = line18ZDFT_TotalRunningTime1;

                                //累计运行次数
                                Line18ZDFT_TotalRunningCounts line18ZDFT_TotalRunningCounts1 = new Line18ZDFT_TotalRunningCounts();
                                line18ZDFT_TotalRunningCounts1.value = Line18State.totalRunningCounts;
                                line18ZDFT_TotalRunningCounts1.ts = ts;
                                line18DataPointParamInfo.totalRunningCounts = line18ZDFT_TotalRunningCounts1;

                                //电机振动速度
                                Line18ZDFT_MotorVibrationSpeed line18ZDFT_MotorVibrationSpeed1 = new Line18ZDFT_MotorVibrationSpeed();
                                line18ZDFT_MotorVibrationSpeed1.value = Line18State.motorVibrationSpeed;
                                line18ZDFT_MotorVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.motorVibrationSpeed = line18ZDFT_MotorVibrationSpeed1;
                                //减速机振动速度
                                Line18ZDFT_GearboxVibrationSpeed line18ZDFT_GearboxVibrationSpeed1 = new Line18ZDFT_GearboxVibrationSpeed();
                                line18ZDFT_GearboxVibrationSpeed1.value = Line18State.gearboxVibrationSpeed;
                                line18ZDFT_GearboxVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.gearboxVibrationSpeed = line18ZDFT_GearboxVibrationSpeed1;
                                //底座振动速度
                                Line18ZDFT_BaseVibrationSpeed line18ZDFT_BaseVibrationSpeed1 = new Line18ZDFT_BaseVibrationSpeed();
                                line18ZDFT_BaseVibrationSpeed1.value = Line18State.baseVibrationSpeed;
                                line18ZDFT_BaseVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.baseVibrationSpeed = line18ZDFT_BaseVibrationSpeed1;
                                //主驱动轮振动速度
                                Line18ZDFT_MainVibrationSpeed line18ZDFT_MainVibrationSpeed1 = new Line18ZDFT_MainVibrationSpeed();
                                line18ZDFT_MainVibrationSpeed1.value = Line18State.mainVibrationSpeed;
                                line18ZDFT_MainVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.mainVibrationSpeed = line18ZDFT_MainVibrationSpeed1;
                                //梯级链涨紧轮振动速度
                                Line18ZDFT_StepVibrationSpeed line18ZDFT_StepVibrationSpeed1 = new Line18ZDFT_StepVibrationSpeed();
                                line18ZDFT_StepVibrationSpeed1.value = Line18State.stepVibrationSpeed;
                                line18ZDFT_StepVibrationSpeed1.ts = ts;
                                line18DataPointParamInfo.stepVibrationSpeed = line18ZDFT_StepVibrationSpeed1;
                                line18DTDataPoint.ParamsInfo = line18DataPointParamInfo;
                                //string message = JsonConvert.SerializeObject(line18DTDataPoint);

                                string message = JsonConvert.SerializeObject(line18DTDataPoint).Replace("ParamsInfo", "params");
                                if (mqttlog == 1)
                                {
                                    LogHelper.Log(message);
                                }

                                clientBUS.PublicMessage(topic, message, 0);
                            }
                        }
                        else
                        {
                            TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - lastreceive.Ticks);
                            if (secondSpan.TotalMilliseconds > 10000)
                            {
                                long ts = DateTimeOffset.Now.ToUnixTimeSeconds();

                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                byte[] bytes = new byte[4];
                                rng.GetBytes(bytes);
                                Line18DTDataPoint line18DTDataPoint = new Line18DTDataPoint();
                                line18DTDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();

                                //设备代码
                                Line18DataPointParamInfo line18DataPointParamInfo = new Line18DataPointParamInfo();
                                Line18ZDFT_REG_CODE line18ZDFT_REG_CODE = new Line18ZDFT_REG_CODE();
                                line18ZDFT_REG_CODE.value = DeviceCode;
                                line18ZDFT_REG_CODE.ts = ts;
                                line18DataPointParamInfo.REG_COD = line18ZDFT_REG_CODE;

                                //设备识别码
                                Line18ZDFT_identificationNumber line18ZDFT_IdentificationNumber = new Line18ZDFT_identificationNumber();
                                line18ZDFT_IdentificationNumber.value = SerialNumber;
                                line18ZDFT_IdentificationNumber.ts = ts;
                                line18DataPointParamInfo.identificationNumber = line18ZDFT_IdentificationNumber;

                                //数据产生时间
                                Line18ZDFT_timeStamps line18ZDFT_TimeStamps = new Line18ZDFT_timeStamps();
                                line18ZDFT_TimeStamps.value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                line18ZDFT_TimeStamps.ts = ts;
                                line18DataPointParamInfo.timeStamps = line18ZDFT_TimeStamps;
                                if (fTLine18ParamInfo.ZDFT_YX_ZT == null)
                                {
                                    //服务模式
                                    Line18ZDFT_serviceMode line18ZDFT_ServiceMode = new Line18ZDFT_serviceMode();
                                    line18ZDFT_ServiceMode.value = 3;
                                    line18ZDFT_ServiceMode.ts = ts;
                                    line18DataPointParamInfo.serviceMode = line18ZDFT_ServiceMode;
                                    //运行状态
                                    Line18ZDFT_operationStatus line18ZDFT_OperationStatus = new Line18ZDFT_operationStatus();
                                    line18ZDFT_OperationStatus.value = 0;
                                    line18ZDFT_OperationStatus.ts = ts;
                                    line18DataPointParamInfo.operationStatus = line18ZDFT_OperationStatus;
                                    //运行方向
                                    Line18ZDFT_operationDirection line18ZDFT_OperationDirection = new Line18ZDFT_operationDirection();
                                    line18ZDFT_OperationDirection.value = 0;
                                    line18ZDFT_OperationDirection.ts = ts;
                                    line18DataPointParamInfo.operationDirection = line18ZDFT_OperationDirection;
                                    //故障信号
                                    Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ1 = new Line18ZDFT_GZ_BJ();
                                    line18ZDFT_GZ_BJ1.value = false;
                                    line18ZDFT_GZ_BJ1.ts = ts;
                                    line18DataPointParamInfo.faultState = line18ZDFT_GZ_BJ1;
                                    //故障代码
                                    Line18ZDFT_FaultCode line18ZDFT_FaultCode = new Line18ZDFT_FaultCode();
                                    line18ZDFT_FaultCode.value = 0;
                                    line18ZDFT_FaultCode.ts = ts;
                                    line18DataPointParamInfo.faultCode = line18ZDFT_FaultCode;
                                    //预警信号
                                    Line18ZDFT_AlarmState line18ZDFT_AlarmState1 = new Line18ZDFT_AlarmState();
                                    line18ZDFT_AlarmState1.value = false;
                                    line18ZDFT_AlarmState1.ts = ts;
                                    line18DataPointParamInfo.alarmState = line18ZDFT_AlarmState1;
                                    //预警代码
                                    Line18ZDFT_AlarmCode line18ZDFT_AlarmCode1 = new Line18ZDFT_AlarmCode();
                                    line18ZDFT_AlarmCode1.value = 0;
                                    line18ZDFT_AlarmCode1.ts = ts;
                                    line18DataPointParamInfo.alarmCode = line18ZDFT_AlarmCode1;
                                    //累计运行时间
                                    Line18ZDFT_TotalRunningTime line18ZDFT_TotalRunningTime1 = new Line18ZDFT_TotalRunningTime();
                                    line18ZDFT_TotalRunningTime1.value = 0;
                                    line18ZDFT_TotalRunningTime1.ts = ts;
                                    line18DataPointParamInfo.totalRunningTime = line18ZDFT_TotalRunningTime1;
                                    //累计运行次数
                                    Line18ZDFT_TotalRunningCounts line18ZDFT_TotalRunningCounts1 = new Line18ZDFT_TotalRunningCounts();
                                    line18ZDFT_TotalRunningCounts1.value = 0;
                                    line18ZDFT_TotalRunningCounts1.ts = ts;
                                    line18DataPointParamInfo.totalRunningCounts = line18ZDFT_TotalRunningCounts1;
                                    //电机振动速度
                                    Line18ZDFT_MotorVibrationSpeed line18ZDFT_MotorVibrationSpeed1 = new Line18ZDFT_MotorVibrationSpeed();
                                    line18ZDFT_MotorVibrationSpeed1.value = 0;
                                    line18ZDFT_MotorVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.motorVibrationSpeed = line18ZDFT_MotorVibrationSpeed1;
                                    //减速机振动速度
                                    Line18ZDFT_GearboxVibrationSpeed line18ZDFT_GearboxVibrationSpeed1 = new Line18ZDFT_GearboxVibrationSpeed();
                                    line18ZDFT_GearboxVibrationSpeed1.value = 0;
                                    line18ZDFT_GearboxVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.gearboxVibrationSpeed = line18ZDFT_GearboxVibrationSpeed1;
                                    //底座振动速度
                                    Line18ZDFT_BaseVibrationSpeed line18ZDFT_BaseVibrationSpeed1 = new Line18ZDFT_BaseVibrationSpeed();
                                    line18ZDFT_BaseVibrationSpeed1.value = 0;
                                    line18ZDFT_BaseVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.baseVibrationSpeed = line18ZDFT_BaseVibrationSpeed1;
                                    //主驱动轮振动速度
                                    Line18ZDFT_MainVibrationSpeed line18ZDFT_MainVibrationSpeed1 = new Line18ZDFT_MainVibrationSpeed();
                                    line18ZDFT_MainVibrationSpeed1.value = 0;
                                    line18ZDFT_MainVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.mainVibrationSpeed = line18ZDFT_MainVibrationSpeed1;
                                    //梯级链涨紧轮振动速度
                                    Line18ZDFT_StepVibrationSpeed line18ZDFT_StepVibrationSpeed1 = new Line18ZDFT_StepVibrationSpeed();
                                    line18ZDFT_StepVibrationSpeed1.value = 0;
                                    line18ZDFT_StepVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.stepVibrationSpeed = line18ZDFT_StepVibrationSpeed1;
                                }
                                else
                                {
                                    int fx = fTLine18ParamInfo.ZDFT_SX_ZT.value ? 0 : fTLine18ParamInfo.ZDFT_XX_ZT.value ? 1 : fTLine18ParamInfo.ZDFT_TZ_ZT.value ? 2 : 3;
                                    //服务模式
                                    Line18ZDFT_serviceMode line18ZDFT_ServiceMode = new Line18ZDFT_serviceMode();
                                    if (fx == 2 || fTLine18ParamInfo.ZDFT_GZ_BJ.value || fTLine18ParamInfo.ZDFT_JT_BJ.value)
                                    {
                                        line18ZDFT_ServiceMode.value = 0;
                                    }
                                    else if (fTLine18ParamInfo.ZDFT_YX_ZT.value)
                                    {
                                        line18ZDFT_ServiceMode.value = 1;
                                    }
                                    else if (fTLine18ParamInfo.ZDFT_JX_BJ.value)
                                    {
                                        line18ZDFT_ServiceMode.value = 2;
                                    }
                                    else
                                    {
                                        line18ZDFT_ServiceMode.value = 3;
                                    }
                                    line18ZDFT_ServiceMode.ts = ts;
                                    line18DataPointParamInfo.serviceMode = line18ZDFT_ServiceMode;
                                    //运行状态
                                    Line18ZDFT_operationStatus line18ZDFT_OperationStatus = new Line18ZDFT_operationStatus();
                                    if (fTLine18ParamInfo.ZDFT_JT_BJ.value)
                                    {
                                        line18ZDFT_OperationStatus.value = 2;
                                    }
                                    else if (fx == 2 || fTLine18ParamInfo.ZDFT_JX_BJ.value || fTLine18ParamInfo.ZDFT_GZ_BJ.value || fTLine18ParamInfo.ZDFT_JT_BJ.value)
                                    {
                                        line18ZDFT_OperationStatus.value = 0;
                                    }
                                    else
                                    {
                                        line18ZDFT_OperationStatus.value = 1;
                                    }
                                    line18ZDFT_OperationStatus.ts = ts;
                                    line18DataPointParamInfo.operationStatus = line18ZDFT_OperationStatus;
                                    //运行方向
                                    Line18ZDFT_operationDirection line18ZDFT_OperationDirection = new Line18ZDFT_operationDirection();
                                    if (fx == 0)
                                    {
                                        line18ZDFT_OperationDirection.value = 1;
                                    }
                                    else if (fx == 1)
                                    {
                                        line18ZDFT_OperationDirection.value = 2;
                                    }
                                    else
                                    {
                                        line18ZDFT_OperationDirection.value = 0;
                                    }
                                    line18ZDFT_OperationDirection.ts = ts;
                                    line18DataPointParamInfo.operationDirection = line18ZDFT_OperationDirection;
                                    //故障
                                    Line18ZDFT_GZ_BJ line18ZDFT_GZ_BJ1 = new Line18ZDFT_GZ_BJ();
                                    if (fTLine18ParamInfo.ZDFT_GZ_BJ.value)
                                    {
                                        line18ZDFT_GZ_BJ1.value = true;
                                    }
                                    else
                                    {
                                        line18ZDFT_GZ_BJ1.value = false;
                                    }
                                    line18ZDFT_GZ_BJ1.ts = ts;
                                    line18DataPointParamInfo.faultState = line18ZDFT_GZ_BJ1;
                                    //故障代码
                                    Line18ZDFT_FaultCode line18ZDFT_FaultCode = new Line18ZDFT_FaultCode();
                                    if (string.IsNullOrEmpty(fTLine18ParamInfo.ZDFT_GZ_DM.value))
                                    {
                                        line18ZDFT_FaultCode.value = 0;
                                    }
                                    else
                                    {
                                        line18ZDFT_FaultCode.value = Convert.ToInt32(fTLine18ParamInfo.ZDFT_GZ_DM.value);
                                    }
                                    line18ZDFT_FaultCode.ts = ts;
                                    line18DataPointParamInfo.faultCode = line18ZDFT_FaultCode;

                                    //预警信号
                                    Line18ZDFT_AlarmState line18ZDFT_AlarmState1 = new Line18ZDFT_AlarmState();
                                    line18ZDFT_AlarmState1.value = false;
                                    line18ZDFT_AlarmState1.ts = ts;
                                    line18DataPointParamInfo.alarmState = line18ZDFT_AlarmState1;
                                    //预警代码
                                    Line18ZDFT_AlarmCode line18ZDFT_AlarmCode1 = new Line18ZDFT_AlarmCode();
                                    line18ZDFT_AlarmCode1.value = 0;
                                    line18ZDFT_AlarmCode1.ts = ts;
                                    line18DataPointParamInfo.alarmCode = line18ZDFT_AlarmCode1;

                                    //累计运行时间
                                    Line18ZDFT_TotalRunningTime line18ZDFT_TotalRunningTime1 = new Line18ZDFT_TotalRunningTime();
                                    line18ZDFT_TotalRunningTime1.value = 0;
                                    line18ZDFT_TotalRunningTime1.ts = ts;
                                    line18DataPointParamInfo.totalRunningTime = line18ZDFT_TotalRunningTime1;

                                    //累计运行次数
                                    Line18ZDFT_TotalRunningCounts line18ZDFT_TotalRunningCounts1 = new Line18ZDFT_TotalRunningCounts();
                                    line18ZDFT_TotalRunningCounts1.value = 0;
                                    line18ZDFT_TotalRunningCounts1.ts = ts;
                                    line18DataPointParamInfo.totalRunningCounts = line18ZDFT_TotalRunningCounts1;

                                    //电机振动速度
                                    Line18ZDFT_MotorVibrationSpeed line18ZDFT_MotorVibrationSpeed1 = new Line18ZDFT_MotorVibrationSpeed();
                                    line18ZDFT_MotorVibrationSpeed1.value = 0;
                                    line18ZDFT_MotorVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.motorVibrationSpeed = line18ZDFT_MotorVibrationSpeed1;
                                    //减速机振动速度
                                    Line18ZDFT_GearboxVibrationSpeed line18ZDFT_GearboxVibrationSpeed1 = new Line18ZDFT_GearboxVibrationSpeed();
                                    line18ZDFT_GearboxVibrationSpeed1.value = 0;
                                    line18ZDFT_GearboxVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.gearboxVibrationSpeed = line18ZDFT_GearboxVibrationSpeed1;
                                    //底座振动速度
                                    Line18ZDFT_BaseVibrationSpeed line18ZDFT_BaseVibrationSpeed1 = new Line18ZDFT_BaseVibrationSpeed();
                                    line18ZDFT_BaseVibrationSpeed1.value = 0;
                                    line18ZDFT_BaseVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.baseVibrationSpeed = line18ZDFT_BaseVibrationSpeed1;
                                    //主驱动轮振动速度
                                    Line18ZDFT_MainVibrationSpeed line18ZDFT_MainVibrationSpeed1 = new Line18ZDFT_MainVibrationSpeed();
                                    line18ZDFT_MainVibrationSpeed1.value = 0;
                                    line18ZDFT_MainVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.mainVibrationSpeed = line18ZDFT_MainVibrationSpeed1;
                                    //梯级链涨紧轮振动速度
                                    Line18ZDFT_StepVibrationSpeed line18ZDFT_StepVibrationSpeed1 = new Line18ZDFT_StepVibrationSpeed();
                                    line18ZDFT_StepVibrationSpeed1.value = 0;
                                    line18ZDFT_StepVibrationSpeed1.ts = ts;
                                    line18DataPointParamInfo.stepVibrationSpeed = line18ZDFT_StepVibrationSpeed1;
                                }
                                line18DTDataPoint.ParamsInfo = line18DataPointParamInfo;
                                string message = JsonConvert.SerializeObject(line18DTDataPoint).Replace("ParamsInfo", "params"); ;


                                if (mqttlog == 1)
                                {
                                    LogHelper.Log(message);
                                }
                                await clientBUS.PublicMessage(topic, message, 0);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("发送心跳异常" + ex.ToString());
                    }

                    Task.Delay(1000).Wait();
                }
            });
        }

        public void QueryttyS1_Status(string cmd)
        {
            try
            {
                ttyS0SendMsg(cmd);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                return;
            }
        }

        public byte[] Crc18(byte[] buffer, int start = 0, int len = 0)
        {
            if (buffer == null || buffer.Length == 0) return null;
            if (start < 0) return null;
            if (len == 0) len = buffer.Length - start;
            int length = start + len;
            if (length > buffer.Length) return null;
            ushort crc = 0xFFFF;// Initial value
            for (int i = start; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }

        public bool ttyS0SendMsg(string data)
        {
            try
            {
                myS0SerialPort_1.Write(ToBytesSendDate(data), 0, ToBytesSendDate(data).Length);
                //if (data != query_cmd)
                //{
                // LogHelper.Log(data);
                //}
                return true;
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                return false;
            }
        }

        public static byte[] ToBytesSendDate(string str)
        {
            List<byte> result = new List<byte>();
            if (str.Contains(" "))
            {
                string[] strs = str.Trim().Split(' ');
                foreach (string s in strs)
                {
                    result.Add(Convert.ToByte(s.Trim(), 16));
                }
            }
            else
            {
                result.Add(Convert.ToByte(str, 16));
            }
            return result.ToArray();
        }

        public List<FaultPoint> GetAllPoint()
        {
            List<FaultPoint> faultPoints = new List<FaultPoint>();
            //创建xml文档
            XmlDocument xmldoc = new XmlDocument();

            //导入指定xml文件
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释，注释也是节点，但是无法转换成XmlElement，所以这里不忽略，下面转换就报错
                                           //XmlReader是用liu需要using包起来，读完释放资源，防止占用
            using (XmlReader reader = XmlReader.Create("FaultCode.xml", settings))
            {
                xmldoc.Load(reader);
            }

            //获取根节点
            XmlElement faultxml = xmldoc.DocumentElement;

            //遍历根节点的子节点
            foreach (XmlElement fault in faultxml.ChildNodes)
            {

                if (fault.Name.Contains("Fault"))
                {
                    FaultPoint faultPoint = new FaultPoint();
                    faultPoint.id = Convert.ToInt32(fault.Name.Substring(5, fault.Name.Length - 5));
                    faultPoint.faultCode = fault.InnerText;

                    faults_list.Add(faultPoint);
                }
            }
            return faultPoints;
        }

        public void GetQueryCmd()
        {
            byte[] cmd_buffer = new byte[6];
            cmd_buffer[0] = (byte)(liftid & 0xff);
            cmd_buffer[1] = 0x03;
            cmd_buffer[2] = 0x00;
            cmd_buffer[3] = 0x00;
            cmd_buffer[4] = 0x00;
            cmd_buffer[5] = 0x1D;
            byte[] crc = Crc18(cmd_buffer);
            byte[] data_crc = new byte[2];
            data_crc[0] = crc[1];
            data_crc[1] = crc[0];
            query_cmd = ToHexs(cmd_buffer) + ToHexs(data_crc);

            byte[] cmd_buffer2 = new byte[6];
            cmd_buffer2[0] = (byte)(liftid & 0xff);
            cmd_buffer2[1] = 0x03;
            cmd_buffer2[2] = 0x00;
            cmd_buffer2[3] = 0x6D;
            cmd_buffer2[4] = 0x00;
            cmd_buffer2[5] = 0x23;
            byte[] crc2 = Crc18(cmd_buffer2);
            byte[] data_crc2 = new byte[2];
            data_crc2[0] = crc2[1];
            data_crc2[1] = crc2[0];
            query_cmd_2 = ToHexs(cmd_buffer2) + ToHexs(data_crc2);
        }
        #endregion

        #region ttys1

        public static SerialPort myS1SerialPort_2 = new SerialPort();
        private Timer SerialPortRecvTimerS1_2;
        private static int byteLength_2 = 40960;
        public byte[] byteSerialPortRecvS1_2 = new byte[byteLength_2];
        public event EventHandler<SerialPort485_2RecvEventArgs> ReceivedDataEventS1_2;
        private static int pSerialPortRecv_S1_2 = 0;

        public static List<YYData> yy_cmd_list = new List<YYData>();
        public static int yy_id = 0;
        public static bool upload_flag = false;
        public static bool start_flag = false;
        public static bool data_flag = false;
        public static bool data_ack_flag = false;
        public static bool eot_flag = false;
        public static bool end_ack_flag = false;
        public static bool end_flag = false;
        public static bool success = false;
        public static int fail_count = 0;

        public byte old_fail = 0x00;
        private void SPRecvTimer_Tick_ttyS1_2(object sender, EventArgs e)
        {
            byte[] TemporaryData = new byte[byteLength_2];
            int TemporaryDataLength = 0;

            if (ReceivedDataEventS1_2 != null)
            {
                byteSerialPortRecvS1_2.CopyTo(TemporaryData, 0);
                TemporaryDataLength = pSerialPortRecv_S1_2;

                ReceivedDataEventS1_2.Invoke(this, new SerialPort485_2RecvEventArgs(TemporaryData, TemporaryDataLength));
                // 数据处理完后，将指针指向数据头，等待接收新的数据
                pSerialPortRecv_S1_2 = 0;
            }
        }

        public class SerialPort485_2RecvEventArgs : EventArgs
        {
            /// <summary>
            /// 广播 收到的串口数据
            /// </summary>
            public readonly byte[] RecvData_485_2 = new byte[byteLength_2];

            /// <summary>
            /// 收到数据 的 长度
            /// </summary>
            public readonly int RecvDataLength_485_2;

            /// <summary>
            /// 将 收到的数据 转化成 待广播的数据
            /// </summary>
            public SerialPort485_2RecvEventArgs(byte[] recvData, int recvDataLength)
            {
                recvData.CopyTo(RecvData_485_2, 0);
                RecvDataLength_485_2 = recvDataLength;
            }
        }
        public void OpenS1SerialPort(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            try
            {
                SetS1SerialPortConfig(portName, baudRate, parity, dataBits, stopBits);
                myS1SerialPort_2.Open();
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }

        private void SetS1SerialPortConfig(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                portName = $"/dev/{portName}";
            }
            // 串口 参数设置
            myS1SerialPort_2.PortName = portName;
            myS1SerialPort_2.BaudRate = baudRate;
            switch (parity)
            {
                case 0:
                default:
                    myS1SerialPort_2.Parity = Parity.None;
                    break;
                case 1:
                    myS1SerialPort_2.Parity = Parity.Odd;
                    break;
                case 2:
                    myS1SerialPort_2.Parity = Parity.Even;
                    break;
                case 3:
                    myS1SerialPort_2.Parity = Parity.Mark;
                    break;
                case 4:
                    myS1SerialPort_2.Parity = Parity.Space;
                    break;
            }
            myS1SerialPort_2.DataBits = ((4 < dataBits) && (dataBits < 9)) ? dataBits : 8;
            switch (stopBits)
            {
                case 0:
                    myS1SerialPort_2.StopBits = StopBits.None;
                    break;
                case 1:
                default:
                    myS1SerialPort_2.StopBits = StopBits.One;
                    break;
                case 2:
                    myS1SerialPort_2.StopBits = StopBits.OnePointFive;
                    break;
                case 3:
                    myS1SerialPort_2.StopBits = StopBits.Two;
                    break;
            }
            //my485SerialPort_1.ReadTimeout = 200;
            myS1SerialPort_2.RtsEnable = true;
            myS1SerialPort_2.DataReceived += new SerialDataReceivedEventHandler(DataReceived_ttyS1);

            // 串口 接收数据处理定时器 参数设置
            SerialPortRecvTimerS1_2 = new System.Timers.Timer();
            SerialPortRecvTimerS1_2.Interval = 100;
            SerialPortRecvTimerS1_2.AutoReset = false;
            SerialPortRecvTimerS1_2.Elapsed += new ElapsedEventHandler(SPRecvTimer_Tick_ttyS1_2);
        }

        private void DataReceived_ttyS1(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPortRecvTimerS1_2.Stop();

            try
            {
                byte[] ReadBuf = new byte[myS1SerialPort_2.BytesToRead];
                //byte[] ReadBuf = new byte[myS1SerialPort_2.BytesToRead];
                //if (pSerialPortRecv_S1_2 > byteLength_2 - ReadBuf.Length)
                //{
                //    pSerialPortRecv_S1_2 = 0;
                //}
                myS1SerialPort_2.Read(ReadBuf, 0, ReadBuf.Length);

                LogHelper.Log("接收：" + ToHexs(ReadBuf));
                //ReadBuf.CopyTo(byteSerialPortRecvS1_2, pSerialPortRecv_S1_2);
                //pSerialPortRecv_S1_2 += ReadBuf.Length;
                if (upload_flag)
                {
                    if (fail_count > 10)
                    {
                        yy_cmd_list.Clear();
                        upload_flag = false;
                        start_flag = false;
                        data_flag = false;
                        data_ack_flag = false;
                        eot_flag = false;
                        end_ack_flag = false;
                        end_flag = false;
                        success = false;
                        fail_count = 0;
                        yy_id = 0;
                        return;
                    }
                    if (start_flag && ReadBuf[0] == 0x15 || ReadBuf[0] == 0x43)
                    {
                        old_fail = ReadBuf[0];
                        if (old_fail == 0x15 || old_fail == 0x43)
                        {
                            fail_count++;
                        }
                    }
                    if (!start_flag && ReadBuf[0] == 0x43)//发送起始帧
                    {
                        old_fail = 0x00;
                        YYData yYData = yy_cmd_list.FirstOrDefault(x => x.type == "start");
                        bool result = ttyS1SendBuffer(yYData.data);
                        if (result)
                        {
                            start_flag = true;
                        }
                    }
                    else if (!start_flag && ReadBuf[0] == 0x15)
                    {
                        //起始帧发送失败，重新发送起始帧
                        YYData yYData = yy_cmd_list.FirstOrDefault(x => x.type == "start");
                        bool result = ttyS1SendBuffer(yYData.data);
                        if (result)
                        {
                            start_flag = true;
                        }
                    }
                    else if (start_flag && !data_flag && ReadBuf[0] == 0x06 && ReadBuf.Length == 1)//起始帧发送成功
                    {
                        data_ack_flag = true;
                    }
                    else if (data_ack_flag && !data_flag && ReadBuf[0] == 0x43 && ReadBuf.Length == 1)
                    {
                        old_fail = 0x00;
                        //起始帧发送成功，发送第一帧数据帧
                        yy_cmd_list.RemoveAll(x => x.type == "start");
                        List<YYData> yYDatas = yy_cmd_list.FindAll(x => x.type == "data").OrderBy(s => s.package).ToList();
                        if (yYDatas.Count > 0)
                        {
                            bool result = ttyS1SendBuffer(yYDatas[0].data);
                            if (result)
                            {
                                data_flag = true;
                            }
                        }
                    }
                    else if (start_flag && !data_flag && ReadBuf[0] == 0x06 && ReadBuf.Length == 2 && ReadBuf[1] == 0x43)//起始帧发送成功
                    {
                        old_fail = 0x00;
                        //起始帧发送成功，发送第一帧数据帧
                        yy_cmd_list.RemoveAll(x => x.type == "start");
                        List<YYData> yYDatas = yy_cmd_list.FindAll(x => x.type == "data").OrderBy(s => s.package).ToList();
                        if (yYDatas.Count > 0)
                        {
                            bool result = ttyS1SendBuffer(yYDatas[0].data);
                            if (result)
                            {
                                data_flag = true;
                            }
                        }
                    }
                    else if (data_flag && !eot_flag && ReadBuf[0] == 0x15)
                    {
                        //数据帧发送失败，重新发送数据帧
                        List<YYData> yYDatas = yy_cmd_list.FindAll(x => x.type == "data").OrderBy(s => s.package).ToList();
                        if (yYDatas.Count > 0)
                        {
                            ttyS1SendBuffer(yYDatas[0].data);
                        }
                    }
                    else if (data_flag && !eot_flag && ReadBuf[0] == 0x06)//发送数据帧
                    {
                        //发送数据帧
                        List<YYData> yYDatas = yy_cmd_list.FindAll(x => x.type == "data").OrderBy(s => s.package).ToList();

                        //最后一帧数据发送完毕后，连发两帧EOT
                        if (yYDatas.Count > 0)
                        {
                            yy_cmd_list.Remove(yYDatas[0]);
                            yYDatas = yy_cmd_list.FindAll(x => x.type == "data").OrderBy(s => s.package).ToList();
                            if (yYDatas.Count > 0)
                            {
                                ttyS1SendBuffer(yYDatas[0].data);
                            }
                            else
                            {
                                byte[] eot_data = new byte[1] { 0x04 };
                                bool result = ttyS1SendBuffer(eot_data);
                                if (result)
                                {
                                    eot_flag = true;
                                }
                            }
                        }
                        else
                        {
                            byte[] eot_data = new byte[1] { 0x04 };
                            bool result = ttyS1SendBuffer(eot_data);
                            if (result)
                            {
                                eot_flag = true;
                            }
                        }
                    }
                    else if (eot_flag && ReadBuf[0] == 0x15)
                    {
                        //重复发送eot数据
                        byte[] eot_data = new byte[1] { 0x04 };
                        bool result = ttyS1SendBuffer(eot_data);
                    }
                    else if (eot_flag && !end_flag && ReadBuf.Length == 1 && ReadBuf[0] == 0x06)
                    {
                        //eot发送成功
                        end_ack_flag = true;
                    }
                    else if (end_ack_flag && !end_flag && ReadBuf.Length == 1 && ReadBuf[0] == 0x43)
                    {
                        //eot发送成功，发送结束帧
                        byte[] end_bytes = Enumerable.Repeat((byte)0x00, 133).ToArray();
                        end_bytes[0] = 0x01;
                        end_bytes[2] = 0xFF;
                        bool result = ttyS1SendBuffer(end_bytes);
                        if (result)
                        {
                            end_flag = true;
                        }
                    }
                    else if (eot_flag && !end_flag && ReadBuf.Length == 2 && ReadBuf[0] == 0x06 && ReadBuf[1] == 0x43)
                    {
                        old_fail = 0x00;
                        //eot发送成功，发送结束帧
                        end_ack_flag = true;
                        byte[] end_bytes = Enumerable.Repeat((byte)0x00, 133).ToArray();
                        end_bytes[0] = 0x01;
                        end_bytes[2] = 0xFF;
                        bool result = ttyS1SendBuffer(end_bytes);
                        if (result)
                        {
                            end_flag = true;
                        }
                    }
                    else if (end_flag && ReadBuf[0] == 0x06)
                    {
                        //结束帧发送成功，上传成功
                        yy_cmd_list.Clear();
                        upload_flag = false;
                        start_flag = false;
                        data_flag = false;
                        data_ack_flag = false;
                        eot_flag = false;
                        end_ack_flag = false;
                        end_flag = false;
                        success = false;
                        fail_count = 0;

                        SwitchingProtocol(2);
                        AudioDAL audioDAL = new AudioDAL();
                        audioDAL.UpdateAudioState(yy_id);
                        yy_id = 0;
                    }
                    else if (end_flag && ReadBuf[0] == 0x15)
                    {
                        //结束帧发送失败，重新发送
                        byte[] end_bytes = Enumerable.Repeat((byte)0x00, 133).ToArray();
                        end_bytes[0] = 0x01;
                        end_bytes[0] = 0x02;
                        bool result = ttyS1SendBuffer(end_bytes);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            SerialPortRecvTimerS1_2.Start();
        }

        public static bool ttyS1SendBuffer(byte[] data)
        {
            try
            {
                myS1SerialPort_2.Write(data, 0, data.Length);
                LogHelper.Log("发送:" + ToHexs(data));
                return true;
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                return false;
            }
        }

        public static bool ttyS1SendMsg(string data)
        {
            try
            {
                myS1SerialPort_2.Write(ToBytesSendDate(data), 0, ToBytesSendDate(data).Length);
                LogHelper.Log("发送:" + data);
                return true;
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                return false;
            }
        }

        //切换协议 1=YMODEM;2=MODBUS
        public static bool SwitchingProtocol(int mode)
        {
            try
            {
                string cmd = string.Empty;
                if (mode == 1)
                {
                    cmd = "01 10 00 16 00 01 02 00 01 65 66";
                }
                else
                {
                    cmd = "01 10 00 16 00 01 02 00 00 A4 A6";
                }
                return ttyS1SendMsg(cmd);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                return false;
            }
        }

        #endregion
    }
}
